Given preorder and inorder traversal of a tree, construct the binary tree. Note: |
|
Hint: About Duplicates: Consider the following case:
We can construct the following trees which are both perfectly valid solutions. 7 7 / or \ 7 7 Clearly, there would be ambiguity in constructing the tree if duplicates were allowed. Solution: _______7______ / \ __10__ ___2 / \ / 4 3 _8 \ / 1 11 The preorder and inorder traversals for the binary tree above is:
The crucial observation to this problem is the tree's root always coincides with the first element in preorder traversal. This must be true because in preorder traversal you always traverse the root node before its children. The root node's value appear to be 7 from the binary tree above. We easily find that 7 appears as the 4th index in the inorder sequence. (Notice that earlier we assumed that duplicates are not allowed in the tree, so there would be no ambiguity). For inorder traversal, we visit the left subtree first, then root node, and followed by the right subtree. Therefore, all elements left of 7 must be in the left subtree and all elements to the right must be in the right subtree. We see a clear recursive pattern from the above observation. After creating the root node (7), we construct its left and right subtree from inorder traversal of We left out some details on how we search the root value's index in the inorder sequence. How about a simple linear search? If we assume that the constructed binary tree is always balanced, then we can guarantee the run time complexity to be O(N log N), where N is the number of nodes. However, this is not necessarily the case and the constructed binary tree can be skewed to the left/right, which has the worst complexity of O(N2). A more efficient way is to eliminate the search by using an efficient look-up mechanism such as hash table. By hashing an element's value to its corresponding index in the inorder sequence, we can do look-ups in constant time. Now, we need only O(N) time to construct the tree, which theoretically is the most efficient way. For illustration purpose, the below code uses a simple array for table look-up, which is restricted to elements of 0 to 255 only. You should be able to extend it easily to use a hash table.
Now, if we are given inorder and postorder traversal, can we construct the binary tree? The answer is yes, using very similar approach as above. Please see question Further Thoughts:
nice answer "Therefore, the left and right subtree's postorder traversal must be [10, 4, 3, 1] and [2, 8, 11] respectively." It should be preorder, not postorder. |
|
|
|
}; |
recursively construct left and right public class Solution { public TreeNode buildTree(int[] inorder, int[] postorder) { // Start typing your Java solution below // DO NOT write main() function
} |
1)the first node in preorder is the tree root; 2)find the root node in inorder, then get the left and right subtree order(both preorder and inorder list); 3)if can't find the root node in inorder, then the input is error, can not build the tree;
|
Here is an O(n) iterative solution, using a Stack to keep track of where the subtree roots are.
|
reverse_idx is a hash table reversing the key and value of inorder.
|