Problem Solutions Flashcards
1
Q
Validate BST:
“Implement a function to check if a binary tree is a binary search tree”
A
- In-order traversals of BST are always increasing. Leverage this property. (cannot handle duplicate vals)
- Min/Max solution. Leverage definition of binary tree. Pass min/max values and update the bounds as recursion (or iteration) continues
2
Q
Successor:
“Write an algorithm to find the ‘next’ node (i.e. in-order successor) of a given binary search tree. You may assume that each node has a link to its parent.
A
- If node has node.right, then go left as possible down node.right.
- If node does not have node.right, traverse up the tree until you find a node x where x.left = node. x is the successor. If we reach the root and no such x exists, return None.
Note: this is effectively a database cursor.