Tripadvisor Interview Question

implement a function on a binary search that receives two nodes and returns its common ancestor.

Interview Answers

Anonymous

Jul 12, 2015

Did you mean binary search tree? If yes, we can do next. Take first node, go up until the root and mark visited nodes (visited[cur_node] = true or something like that). Then do the same thing for the second node. Go up until we meet already visited node. This will be the answer.

Anonymous

Nov 19, 2015

Assuming its a Binary Search Tree and you are given the root node. Check if the root value is between the node values. If that true than root is the common ancestor. Else if the root value is greater than both the node values repeat the same procedure with root.left. else repeat the same procedure with root.right.