Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
給出二叉樹的後根遍歷和中根遍歷,構造二叉樹。
瞭解後根遍歷和中根遍歷的特點。
每次在後根遍歷陣列中最右邊就是二叉樹的根,然後去中根遍歷陣列中找到根,根左邊的就是左子樹,根右邊的就是右子樹,對於左子樹和右子樹採取同樣的求法:去後根遍歷陣列中找根,利用中根遍歷陣列劃分左右子樹,遞迴求解。
class Solution {
public:
TreeNode* help(vector<int>& postorder,int l1, int r1, vector<int>& inorder, int l2, int r2)
{
if(l1>r1)
return nullptr;
else
{
int i;
TreeNode* root=new TreeNode(postorder[r1]);
for(i=l2; i<=r2; ++i)
{
if(inorder[i]==postorder[r1])
break;
}
root->left=help(postorder,l1, l1+i-1-l2, inorder, l2, i-1);
root->right=help(postorder,l1+i-l2, r1-1, inorder, i+1, r2);
return root;
}
}
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
return help(postorder, 0, postorder.size()-1,inorder, 0, inorder.size()-1);
}
};
相關文章
- Construct Binary Tree from Inorder and Postorder TraversalStruct
- Leetcode Construct Binary Tree from Inorder and Postorder TraversalLeetCodeStruct
- Construct Binary Tree from Inorder and Postorder Traversal Traversal leetcode javaStructLeetCodeJava
- Construct Binary Tree from Preorder and Inorder TraversalStruct
- Leetcode Construct Binary Tree from Preorder and Inorder TraversalLeetCodeStruct
- Leetcode-Construct Binary Tree from inorder and postorder travesalLeetCodeStruct
- Construct Binary Tree from Preorder and Inorder Traversal leetcode javaStructLeetCodeJava
- Leetcode 105 Construct Binary Tree from Preorder and Inorder TraversalLeetCodeStruct
- Leetcode Binary Tree Postorder TraversalLeetCode
- Leetcode Binary Tree Inorder TraversalLeetCode
- Leetcode-Construct Binary Tree from inorder and preorder travesalLeetCodeStruct
- Leetcode-Binary Tree Postorder TraversalLeetCode
- Binary Tree Postorder Traversal leetcode javaLeetCodeJava
- [leetcode]binary-tree-inorder-traversalLeetCode
- Leetcode-Binary Tree Inorder TraversalLeetCode
- Binary Tree Inorder Traversal leetcode javaLeetCodeJava
- LeetCode 94 [Binary Tree Inorder Traversal]LeetCode
- Leetcode 94. Binary Tree Inorder TraversalLeetCode
- LeetCode | 145. Binary Tree Postorder TraversalLeetCode
- Leetcode 145. Binary Tree Postorder TraversalLeetCode
- LeetCode145:Binary Tree Postorder TraversalLeetCode
- Leetcode-145題:Binary Tree Postorder TraversalLeetCode
- 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)LeetCode
- 【LeetCode】Construct Binary Tree from Preorder and Inorder Traversal 根據先序序列和中序序列恢復二叉樹LeetCodeStruct二叉樹
- Construct Binary Tree from Preorder and Inorder Traversal(前序遍歷和中序遍歷樹構造二叉樹)...Struct二叉樹
- Construct String from Binary TreeStruct
- LeetCode之Construct String from Binary Tree(Kotlin)LeetCodeStructKotlin
- leetcode - Binary Tree Preorder TraversalLeetCode
- Leetcode Binary Tree Preorder TraversalLeetCode
- Binary Tree Level Order Traversal [LEETCODE]LeetCode
- Leetcode Binary Tree Level Order TraversalLeetCode
- Binary Tree Preorder Traversal leetcode javaLeetCodeJava
- LeetCode-Binary Tree Vertical Order TraversalLeetCode
- Leetcode Binary Tree Level Order Traversal IILeetCode
- Leetcode-Binary Tree Level Order TraversalLeetCode
- Binary Tree Level Order Traversal leetcode javaLeetCodeJava
- LeetCode | 144. Binary Tree Preorder TraversalLeetCode
- Leetcode 144. Binary Tree Preorder TraversalLeetCode