LeetCode-Construct Binary Tree from Inorder and Postorder Traversal
2014-08-04 14:00
295 查看
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
Solution:
Code:
<span style="font-size:14px;">/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *helper(const vector<int> &inorder, const vector<int> &postorder, int inBegin, int inEnd, int postBegin, int postEnd) {
if (inBegin > inEnd) return NULL;
TreeNode *root = new TreeNode(postorder[postEnd]);
int index = inBegin;
for (; index <= inEnd; ++index)
if (inorder[index] == postorder[postEnd]) break;
root->left = helper(inorder, postorder, inBegin, index-1, postBegin, index-1-inBegin+postBegin);
root->right = helper(inorder, postorder, index+1, inEnd, index-inBegin+postBegin, postEnd-1);
return root;
}
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
return helper(inorder, postorder, 0, inorder.size()-1, 0, postorder.size()-1);
}
};</span>
Note:
You may assume that duplicates do not exist in the tree.
Solution:
Code:
<span style="font-size:14px;">/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *helper(const vector<int> &inorder, const vector<int> &postorder, int inBegin, int inEnd, int postBegin, int postEnd) {
if (inBegin > inEnd) return NULL;
TreeNode *root = new TreeNode(postorder[postEnd]);
int index = inBegin;
for (; index <= inEnd; ++index)
if (inorder[index] == postorder[postEnd]) break;
root->left = helper(inorder, postorder, inBegin, index-1, postBegin, index-1-inBegin+postBegin);
root->right = helper(inorder, postorder, index+1, inEnd, index-inBegin+postBegin, postEnd-1);
return root;
}
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
return helper(inorder, postorder, 0, inorder.size()-1, 0, postorder.size()-1);
}
};</span>
相关文章推荐
- LeetCode_Construct Binary Tree from Inorder and Postorder Traversal
- leetcode 110: Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- functionclass[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: 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]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 79] 106 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--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 Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode 106: Construct Binary Tree from Inorder and Postorder Traversal