【Leetcode】【Medium】Construct Binary Tree from Inorder and Postorder Traversal
2015-05-21 06:02
507 查看
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
解题思路:
给出一个二叉树的中序和后序遍历结果,还原这个二叉树。
对于一个二叉树:
后序遍历结果为:4 5 2 6 7 3 1
中序遍历结果为:4 2 5 1 6 3 7
由此可以发现规律:
1、后序遍历的最后一个字符,就是根结点(1)
2、发现根节点后,对应在中序遍历中的位置,则在中序遍历队列中,根节点左边的元素构成根的左子树,根的右边元素构成根的右子树;
3、递归的将左右子树也按照上述规律进行构造,最终还原二叉树。
代码:
Note:
You may assume that duplicates do not exist in the tree.
解题思路:
给出一个二叉树的中序和后序遍历结果,还原这个二叉树。
对于一个二叉树:
1 / \ 2 3 / \ / \ 4 5 6 7
后序遍历结果为:4 5 2 6 7 3 1
中序遍历结果为:4 2 5 1 6 3 7
由此可以发现规律:
1、后序遍历的最后一个字符,就是根结点(1)
2、发现根节点后,对应在中序遍历中的位置,则在中序遍历队列中,根节点左边的元素构成根的左子树,根的右边元素构成根的右子树;
3、递归的将左右子树也按照上述规律进行构造,最终还原二叉树。
代码:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { return buildSubtree(postorder, inorder, 0, postorder.size()-1, 0, inorder.size()-1); } TreeNode* buildSubtree(vector<int>& postorder, vector<int>& inorder, int p_left, int p_right, int i_left, int i_right) { if (p_left > p_right || i_left > i_right) return NULL; int root = postorder[p_right]; TreeNode* node = new TreeNode(postorder[p_right]); int range = 0; for (int j = i_left; j <= i_right; ++j) { if (root == inorder[j]) { range = j - i_left; break; } } node->left = buildSubtree(postorder, inorder, p_left, p_left + range - 1, i_left, i_left + range - 1); node->right = buildSubtree(postorder, inorder, p_left + range, p_right - 1, i_left + range + 1, i_right); return node; } };
相关文章推荐
- [Leetcode 106, Medium] Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: 106_Construct Binary Tree from Inorder and Postorder Traversal | 根据中序和后序遍历构建二叉树 | Medium
- 【LeetCode】Construct Binary Tree From Inorder And Postorder Traversal 中序遍历和后序遍历树构造二叉树 - Medium
- LeetCode-106:Construct Binary Tree from Inorder and Postorder Traversal (利用中序和后序遍历构建二叉树) -- medium
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal (Medium) (cpp)
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
- leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree f
- LeetCode 105, 106. Construct Binary Tree from Preorder and Inorder/Inorder and Postorder Traversal
- leetcode 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
- Construct Binary Tree from Inorder and Postorder Traversal - LeetCode 106
- Leetcode 106 Construct Binary Tree from Inorder and Postorder Traversal
- leetcode_middle_94_106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
- leetcode--Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode | Construct Binary Tree from Inorder and Postorder Traversal
- [Leetcode 105, Medium] Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]109. Construct Binary Tree from Inorder and Postorder Traversal由中序序列和后序序列重建二叉树