Coding question

Published on 2020-09-20 by Ashok Be

BST question

You are asked to use an existing binary tree to generate a binary search tree (BST).

Write the code snippet that will take an existing binary tree and the above described functions to generate a valid binary search tree (BST).








































Answer

We can ignore whether a binary tree is a valid BST or not. Just sort the list and build a balanced BST out of it.

  alist = preorder(root)
  alist.sort()
  bbst = build_balanced_bst(alist)
  print("generated BBST", bbst)
Show comments