[LeetCode#105]Construct Binary Tree from Preorder and Inorder Traversal
2015-01-08 10:53
337 查看
The problem:
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
My analysis:
The idea behind this problem is really elegant and meaningful!
We need to combinely use both preorder and inorder arrays.
Rucursion:
The first node in preorder[] must be the root of preorder array.
Use this information, we could separate the inorder[] into left sub-tree and right sub-tree.
One important thing we need to hold is that, we should guarantee the elements of preorder array and inorder array are the same!
How to guarantee it?
we should use the connection between preorder and inorder traversal.
prorder series: ABCDEFGH
inorder series: BCDAEFGH
from the preorder series, we could conclude that A is the root of those nodes. from the inorder series, we could conclude that BCD are the nodes belonged to left sub-tree, and EFGH are the nodes belonged to the right sub-tree.
Even though we can't assure the exeact poistion of BCD in the left sub-tree, we can assure that they must appear ahead of all nodes in the right sub-tree. Thus BCD must appear ahead in the preorder series before EFGH. (the left sub-tree elements must appear before right sub-tree elements in both prorder series and inorder series)
By using the above inference, we could guarantee we can get the same set of elements both in preorder series and inorder series.
(the elements in the left sub-tree must equal to elements both in inorder series and preorder series)
note: In preorder series, we chop off the first element. while in inorder series, we chop off the index(th) element.
left sub-trees preorder series:
What an interesting question !
My solution:
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
My analysis:
The idea behind this problem is really elegant and meaningful!
We need to combinely use both preorder and inorder arrays.
Rucursion:
The first node in preorder[] must be the root of preorder array.
Use this information, we could separate the inorder[] into left sub-tree and right sub-tree.
One important thing we need to hold is that, we should guarantee the elements of preorder array and inorder array are the same!
How to guarantee it?
we should use the connection between preorder and inorder traversal.
prorder series: ABCDEFGH
inorder series: BCDAEFGH
from the preorder series, we could conclude that A is the root of those nodes. from the inorder series, we could conclude that BCD are the nodes belonged to left sub-tree, and EFGH are the nodes belonged to the right sub-tree.
Even though we can't assure the exeact poistion of BCD in the left sub-tree, we can assure that they must appear ahead of all nodes in the right sub-tree. Thus BCD must appear ahead in the preorder series before EFGH. (the left sub-tree elements must appear before right sub-tree elements in both prorder series and inorder series)
By using the above inference, we could guarantee we can get the same set of elements both in preorder series and inorder series.
[inorder_low, index - 1] [index + 1, high] we could infer the sections in preorder series. preorder_high - preorder_low - 1 = index - 1 - inorder_low ====> preoder_high = preorder_low + index - inorder_low
(the elements in the left sub-tree must equal to elements both in inorder series and preorder series)
note: In preorder series, we chop off the first element. while in inorder series, we chop off the index(th) element.
left sub-trees preorder series:
[preorder_low + 1, preorder_low + index - inorder_low] right sub-trees preorder series: [preorder_low + index - inorder_low + 1, preorder_high]
What an interesting question !
My solution:
public class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { if (preorder == null || preorder.length == 0) return null; if (inorder == null || inorder.length == 0) return null; int len = inorder.length; HashMap<Integer, Integer> order_map = new HashMap<Integer, Integer>(); for (int i = 0; i < inorder.length; i++ ) { order_map.put(inorder[i], i); } return helper(preorder, 0, len - 1, inorder, 0, len - 1, order_map); } private TreeNode helper(int[] preorder, int pre_low, int pre_high, int[] inorder, int in_low, int in_high, HashMap<Integer, Integer> order_map) { if (pre_low > pre_high || in_low > in_high) return null; TreeNode ret = new TreeNode(preorder[pre_low]); int index = order_map.get(preorder[pre_low]); ret.left = helper(preorder, pre_low + 1, index - in_low + pre_low, inorder, in_low, index - 1, order_map); ret.right = helper(preorder, index - in_low + pre_low + 1, pre_high, inorder, index + 1, in_high, order_map); return ret; } }
相关文章推荐
- [LeetCode 105] Construct Binary Tree from Preorder and Inorder Traversal
- [JAVA]LeetCode105 Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode_105 Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode(105) Construct Binary Tree from Preorder and Inorder Traversal
- Java for LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode 105/106 Construct Binary Tree from Preorder/Postorder and Inorder Traversal
- [Leetcode 105, Medium] Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode(105) Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal [105]
- [Leetcode 78] 105 Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]105 Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode#105 Construct Binary Tree from Preorder and Inorder Traversal
- leetcode_105_Construct Binary Tree from Preorder and Inorder Traversal
- leetcode || 105、Construct Binary Tree from Preorder and Inorder Traversal
- leetcode 105 —— Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode 105] Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode:105_Construct Binary Tree from Preorder and Inorder Traversal | 根据前序和中序遍历构建二叉树 | Medium
- leetcode 105: Construct Binary Tree from Preorder and Inorder Traversal