給前中後其中二序重構二叉樹
阿新 • • 發佈:2021-02-01
摘自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);
}
};