Problem
Given preorder and inorder traversal of a tree, construct the binary tree.
Example
1 | preorder = [3,9,20,15,7] |
Return the following binary tree:
1 | 3 |
Note
You may assume that duplicates do not exist in the tree.
Solution1
Method: Divide and Conquer (recursive)
Time Complexity: O(n)
Space Complexity:
1 | # Definition for a binary tree node. |
Solution2
Method: Divide and Conquer (Iterator and map)
Time Complexity:
Space Complexity:
1 | class Solution(object): |