Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
2018-02-08 09:12
387 查看
原题:
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
For example, giveninorder = [9,3,15,20,7]
postorder = [9,15,7,20,3]Return the following binary tree: 3
/ \
9 20
/ \
15 7
解决方法:
- 后序遍历的最后一个节点是根节点。
- 根据根节点把中序遍历的点分成左右两部分。
- 根据中序遍历左右部分的长度将后序遍历也分割成左右两部分。
这样递归循环即可求出最后的树。
代码:
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
For example, giveninorder = [9,3,15,20,7]
postorder = [9,15,7,20,3]Return the following binary tree: 3
/ \
9 20
/ \
15 7
解决方法:
- 后序遍历的最后一个节点是根节点。
- 根据根节点把中序遍历的点分成左右两部分。
- 根据中序遍历左右部分的长度将后序遍历也分割成左右两部分。
这样递归循环即可求出最后的树。
代码:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder, int in_start, int in_end, int post_start, int post_end) { if (post_end < post_start) return NULL; TreeNode* root = new TreeNode(postorder[post_end]); int pos; for(int i = in_start; i <= in_end;i++){ if (inorder[i] == postorder[post_end]){ pos = i; break; } } root->left = buildTree(inorder, postorder, in_start, pos -1, post_start, post_start + pos - in_start - 1); root->right = buildTree(inorder, postorder, pos + 1, in_end, post_start + pos - in_start , post_end-1); return root; } TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { return buildTree(inorder, postorder, 0, inorder.size() -1, 0, postorder.size() -1); }
相关文章推荐
- LeetCode 106: Construct Binary Tree from Inorder and Postorder Traversal
- leetcode题解-105 && 106. Construct Binary Tree from Preorder (PostOrder) and Inorder Traversal
- LeetCode *** 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode(106)Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode 106] Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
- 【LeetCode-106】Construct Binary Tree from Inorder and Postorder Traversal
- [JAVA]LeetCode106 Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode(106) Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode-106:Construct Binary Tree from Inorder and Postorder Traversal (利用中序和后序遍历构建二叉树) -- medium
- leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode 105/106 Construct Binary Tree from Preorder/Postorder and Inorder Traversal
- leetcode 106 Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode——106. Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode #106 Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode_c++:Construct Binary Tree from Inorder and Postorder Traversal (106)
- LeetCode 106: Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal
- leetcode || 106、Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal