Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
Pick the first elemnet of Pre[] and search for it in In[]. Make it the root of the tree. LEft of the node contains the elements of In[] before the root (repeat the process recuursively with new Pre[] containing the same number of elements as the In[] for left and contain the elements from left to right).....Similarly for right node
Pick the first elemnet of Pre[] and search for it in In[]. Make it the root of the tree. LEft of the node contains the elements of In[] before the root (repeat the process recuursively with new Pre[] containing the same number of elements as the In[] for left and contain the elements from left to right).....Similarly for right node
Essentially the idea is the same as what candis said. Here is the code for same which I implemented in Java.
I Since the inorder is contiguous, the trick here is to find the start and end elements in the Pre-Order which are to the left of the current root node in the in-order sequence and pass it recursively to construct left and right subtrees
- dilip.rangarajan April 21, 2012