Amazon Interview Question
Software Engineer / Developersuse the characteristic that the IN-ODER TRAVERSAL result of a BST is ASCENDING. then it's straitforward........
yea straitforward. except the crazy interview said it was a binary tree, not a bst. Perhaps we should put him in a strait jacket?
Hows that.. Lets say it is a BST.. how would an inorder traversal exactly help? The traversal would include left node , root and right node even at the leaf(last) level.. And for the sum we would want to do root and anyone of its child till we find a leaf.. Could you explain? Just curious..
Using Depth First Search (DFS) with two parameters traverse(node, sum). When node = leaf check whether sum equal to given sum (SUM). Along the path whenever the sum exceeds SUM stop that branching.
- Silence December 29, 2009The total running time is O(V+E) and space is O(V).