摘自889题解:https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/solution/kan-wo-jiu-gou-liao-san-chong-bian-li-fang-shi-gou/
给前序+后序重构二叉树:
/**
* 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* fun(vector<int>& pre, vector<int>& post,int preBegin,int preEnd, int postBegin, int postEnd){
if(preBegin > preEnd || postBegin > postEnd) return NULL;
TreeNode *root = new TreeNode(pre[preBegin]);
if(preBegin == preEnd ) return root;
int index = postBegin;
while(post[index] != pre[preBegin+1]){
index++;
}
root->left = fun(pre,post,preBegin+1,preBegin+1+(index-postBegin),postBegin,index);
root->right = fun(pre,post,preBegin+1+(index-postBegin)+1,preEnd,index+1,postEnd-1);
return root;
}
TreeNode* constructFromPrePost(vector<int>& pre, vector<int>& post) {
return fun(pre,post,0,pre.size()-1,0,post.size()-1);
}
};
给前序+中序重构二叉树:
LC105
注意:
1. 事实证明边界条件必须是大于号:if(preBegin > preEnd || inBegin > inEnd)
2. 注意递归的边界这里要index-1-inbegin,上一个给前序后序的也不一样,本质都是左子树根位置-左子树起始位置。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* fun(vector<int> &pre, vector<int> &in, int preBegin,int preEnd,int inBegin,int inEnd){
if(preBegin > preEnd || inBegin > inEnd) return nullptr;
TreeNode *root = new TreeNode(pre[preBegin]);
if(preBegin == preEnd) return root;
int index = inBegin;
while(in[index] != pre[preBegin]){
index++;
}
root->left = fun(pre,in,preBegin+1,preBegin+1+(index-1-inBegin), inBegin,index-1);
root->right = fun(pre,in,preBegin+1+(index-1-inBegin)+1,preEnd,index+1,inEnd);
return root;
}
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
return fun(preorder,inorder,0,preorder.size()-1,0,inorder.size()-1);
}
};
给中序+后序重建二叉树:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* fun(vector<int> &in,vector<int> &post,int inBegin,int inEnd,int postBegin,int postEnd){
if(inBegin>inEnd || postBegin > postEnd) return NULL;
TreeNode *root = new TreeNode(post[postEnd]);
if(postBegin == postEnd) return root;
int index = inBegin;
while(in[index] != post[postEnd]){
index++;
}
root->left = fun(in,post,inBegin,index-1,postBegin,postBegin+(index-1-inBegin));
root->right = fun(in,post,index+1,inEnd,postBegin+(index-1-inBegin)+1,postEnd-1);
return root;
}
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
return fun(inorder,postorder,0,inorder.size()-1,0,postorder.size()-1);
}
};