Amazon Interview Question
SDE-2sCountry: India
Interview Type: Written Test
You just need to construct the tree. No need to do an LCA.
Use an adjacency list as shown in anon's code and build a binary tree or adjacency map to represent the relationships. Former is preferred since the graph is very sparse (max three edges per node - 2 for child, 1 for parent).
You could also use an array representation to represent the tree such that 2*i and 2*i+1 are children of i item. However, it would waste space when the graph is linear like a list.
- Anonymous July 04, 2016