Amazon Interview Question

find LCA for two nodes of a binary tree.

Interview Answers

Anonymous

Feb 10, 2012

List 1: tree nodes inorder List 2: tree nodes postorder List 3: all the nodes in between the given 2 nodes List 4: all the nodes after the given 2 nodes the LCA is the common node in List 3 and List 4.

Anonymous

Jul 25, 2012

I think there's an easier solution: List1: the parents of node1 in order bottom to top (can get them by navigating up tree from node 1). Navigate up tree from node2. First parent of node 2 found in list1 is LCA.

2

Anonymous

Oct 19, 2014

The advantage? Well complexity is log (n) and there is no extra memory required i.e. no list creation needed