题目描述
问题求解
递归建树
TreeNode* reConstructBinaryTree(vector<int>& pre,vector<int>& vin) {
int n=pre.size();
if(!n) return nullptr;
auto *root=new TreeNode(pre[0]);
for(int i=0;i<n;++i){
if(pre[0]==vin[i]){
vector<int> leftpre(pre.begin()+1,pre.begin()+i+1);
vector<int> leftvin(vin.begin(),vin.begin()+i);
TreeNode* l=reConstructBinaryTree(leftpre, leftvin);
root->left=l;
vector<int> rightpre(pre.begin()+i+1,pre.end());
vector<int> rightvin(vin.begin()+i+1,vin.end());
TreeNode* r=reConstructBinaryTree(rightpre, rightvin);
root->right=r;
break;
}
}
return root;
}
unordered_map<int,int> hash;
TreeNode* reConstructBinaryTree(vector<int>& pre,int l1,int r1,vector<int>& vin,int l2,int r2) {
if(l1>r1) return nullptr;
auto *root=new TreeNode(pre[l1]);
int mid=hash[pre[l1]];
TreeNode* l=reConstructBinaryTree(pre, l1+1, l1-l2+mid, vin, l2, mid-1);
root->left=l;
TreeNode* r=reConstructBinaryTree(pre, l1-l2+mid+1, r1, vin,mid+1, r2);
root->right=r;
return root;
}
vector<int> solve(vector<int>& xianxu, vector<int>& zhongxu) {
int n=xianxu.size();
for(int i=0;i<n;++i) hash[zhongxu[i]]=i;
TreeNode* root=reConstructBinaryTree(xianxu,0,n-1,zhongxu,0,n-1);
return Traversal(root);
}
层序遍历
vector<int> Traversal(TreeNode* root){
vector<int> res;
if(!root) return res;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
int n=q.size();
for(int i=0;i<n;++i){
TreeNode* cur=q.front();
q.pop();
if(cur->left) q.push(cur->left);
if(cur->right) q.push(cur->right);
if(i==n-1) res.push_back(cur->val);
}
}
return res;
}
完整核心代码
TreeNode* reConstructBinaryTree(vector<int>& pre,vector<int>& vin) {
int n=pre.size();
if(!n) return nullptr;
auto *root=new TreeNode(pre[0]);
for(int i=0;i<n;++i){
if(pre[0]==vin[i]){
vector<int> leftpre(pre.begin()+1,pre.begin()+i+1);
vector<int> leftvin(vin.begin(),vin.begin()+i);
TreeNode* l=reConstructBinaryTree(leftpre, leftvin);
root->left=l;
vector<int> rightpre(pre.begin()+i+1,pre.end());
vector<int> rightvin(vin.begin()+i+1,vin.end());
TreeNode* r=reConstructBinaryTree(rightpre, rightvin);
root->right=r;
break;
}
}
return root;
}
vector<int> Traversal(TreeNode* root){
vector<int> res;
if(!root) return res;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
int n=q.size();
for(int i=0;i<n;++i){
TreeNode* cur=q.front();
q.pop();
if(cur->left) q.push(cur->left);
if(cur->right) q.push(cur->right);
if(i==n-1) res.push_back(cur->val);
}
}
return res;
}
vector<int> solve(vector<int>& xianxu, vector<int>& zhongxu) {
TreeNode* root=reConstructBinaryTree(xianxu,zhongxu);
return Traversal(root);
}
unordered_map<int,int> hash;
TreeNode* reConstructBinaryTree(vector<int>& pre,int l1,int r1,vector<int>& vin,int l2,int r2) {
if(l1>r1) return nullptr;
auto *root=new TreeNode(pre[l1]);
int mid=hash[pre[l1]];
TreeNode* l=reConstructBinaryTree(pre, l1+1, l1-l2+mid, vin, l2, mid-1);
root->left=l;
TreeNode* r=reConstructBinaryTree(pre, l1-l2+mid+1, r1, vin,mid+1, r2);
root->right=r;
return root;
}
vector<int> Traversal(TreeNode* root){
vector<int> res;
if(!root) return res;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
int n=q.size();
for(int i=0;i<n;++i){
TreeNode* cur=q.front();
q.pop();
if(cur->left) q.push(cur->left);
if(cur->right) q.push(cur->right);
if(i==n-1) res.push_back(cur->val);
}
}
return res;
}
vector<int> solve(vector<int>& xianxu, vector<int>& zhongxu) {
int n=xianxu.size();
for(int i=0;i<n;++i) hash[zhongxu[i]]=i;
TreeNode* root=reConstructBinaryTree(xianxu,0,n-1,zhongxu,0,n-1);
return Traversal(root);
}