We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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, given
inorder = [9,3,15,20,7] postorder = [9,15,7,20,3]
Return the following binary tree:
3 / \ 9 20 / \ 15 7
这道题要求从中序和后序遍历的结果来重建原二叉树,我们知道中序的遍历顺序是左-根-右,后序的顺序是左-右-根,对于这种树的重建一般都是采用递归来做,可参见博主之前的一篇博客 Convert Sorted Array to Binary Search Tree。针对这道题,由于后序的顺序的最后一个肯定是根,所以原二叉树的根结点可以知道,题目中给了一个很关键的条件就是树中没有相同元素,有了这个条件就可以在中序遍历中也定位出根节点的位置,并以根节点的位置将中序遍历拆分为左右两个部分,分别对其递归调用原函数。代码如下:
class Solution { public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { return buildTree(inorder, 0, inorder.size() - 1, postorder, 0, postorder.size() - 1); } TreeNode *buildTree(vector<int> &inorder, int iLeft, int iRight, vector<int> &postorder, int pLeft, int pRight) { if (iLeft > iRight || pLeft > pRight) return NULL; TreeNode *cur = new TreeNode(postorder[pRight]); int i = 0; for (i = iLeft; i < inorder.size(); ++i) { if (inorder[i] == cur->val) break; } cur->left = buildTree(inorder, iLeft, i - 1, postorder, pLeft, pLeft + i - iLeft - 1); cur->right = buildTree(inorder, i + 1, iRight, postorder, pLeft + i - iLeft, pRight - 1); return cur; } };
上述代码中需要小心的地方就是递归是 postorder 的左右 index 很容易写错,比如 pLeft + i - iLeft - 1, 这个又长又不好记,首先我们要记住 i - iLeft 是计算 inorder 中根节点位置和左边起始点的距离,然后再加上 postorder 左边起始点然后再减1。我们可以这样分析,如果根结点就是左边起始点的话,那么拆分的话左边序列应该为空集,此时 i - iLeft 为0, pLeft + 0 - 1 < pLeft, 那么再递归调用时就会返回 NULL, 成立。如果根节点是左边起始点紧跟的一个,那么 i - iLeft 为1, pLeft + 1 - 1 = pLeft,再递归调用时还会生成一个节点,就是 pLeft 位置上的节点,为原二叉树的一个叶节点。
下面来看一个例子, 某一二叉树的中序和后序遍历分别为:
Inorder: 11 4 5 13 8 9
Postorder: 11 4 13 9 8 5
11 4 5 13 8 9 => 5
11 4 13 9 8 5 / \
11 4 13 8 9 => 5
11 4 13 9 8 / \
4 8
11 13 9 => 5
11 13 9 / \
/ / \
11 13 9
Github 同步地址:
#106
类似题目:
Construct Binary Tree from Preorder and Postorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal
参考资料:
https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/discuss/758462/C%2B%2B-Detail-Explain-or-Diagram
https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/discuss/34803/Sharing-my-straightforward-recursive-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
The text was updated successfully, but these errors were encountered:
No branches or pull requests
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, given
Return the following binary tree:
这道题要求从中序和后序遍历的结果来重建原二叉树,我们知道中序的遍历顺序是左-根-右,后序的顺序是左-右-根,对于这种树的重建一般都是采用递归来做,可参见博主之前的一篇博客 Convert Sorted Array to Binary Search Tree。针对这道题,由于后序的顺序的最后一个肯定是根,所以原二叉树的根结点可以知道,题目中给了一个很关键的条件就是树中没有相同元素,有了这个条件就可以在中序遍历中也定位出根节点的位置,并以根节点的位置将中序遍历拆分为左右两个部分,分别对其递归调用原函数。代码如下:
上述代码中需要小心的地方就是递归是 postorder 的左右 index 很容易写错,比如 pLeft + i - iLeft - 1, 这个又长又不好记,首先我们要记住 i - iLeft 是计算 inorder 中根节点位置和左边起始点的距离,然后再加上 postorder 左边起始点然后再减1。我们可以这样分析,如果根结点就是左边起始点的话,那么拆分的话左边序列应该为空集,此时 i - iLeft 为0, pLeft + 0 - 1 < pLeft, 那么再递归调用时就会返回 NULL, 成立。如果根节点是左边起始点紧跟的一个,那么 i - iLeft 为1, pLeft + 1 - 1 = pLeft,再递归调用时还会生成一个节点,就是 pLeft 位置上的节点,为原二叉树的一个叶节点。
下面来看一个例子, 某一二叉树的中序和后序遍历分别为:
Inorder: 11 4 5 13 8 9
Postorder: 11 4 13 9 8 5
11 4 5 13 8 9 => 5
11 4 13 9 8 5 / \
11 4 13 8 9 => 5
11 4 13 9 8 / \
4 8
11 13 9 => 5
11 13 9 / \
4 8
/ / \
11 13 9
Github 同步地址:
#106
类似题目:
Construct Binary Tree from Preorder and Postorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal
参考资料:
https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/discuss/758462/C%2B%2B-Detail-Explain-or-Diagram
https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/discuss/34803/Sharing-my-straightforward-recursive-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
The text was updated successfully, but these errors were encountered: