首页 > 代码库 > Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
提交成功的代码:
C++实现:
#include<iostream>#include<new>#include<vector>using namespace std;//Definition for binary treestruct TreeNode{ int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {public: TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { TreeNode *root=NULL; root=build(preorder.begin(),preorder.end(),inorder.begin(),inorder.end()); return root; } TreeNode *build(vector<int>::iterator pbegin,vector<int>::iterator pend,vector<int>::iterator ibegin,vector<int>::iterator iend) { TreeNode *root=NULL; if(pbegin==pend||ibegin==iend) return NULL; auto it=ibegin; while(it!=iend) { if(*it==*pbegin) break; it++; } root=new TreeNode(*pbegin); int len=it-ibegin; root->left=build(pbegin+1,pbegin+1+len,ibegin,it); root->right=build(pbegin+1+len,pend,it+1,iend); return root; } void preorder(TreeNode *root) { if(root) { cout<<root->val<<" "; preorder(root->left); preorder(root->right); } } void inorder(TreeNode *root) { if(root) { inorder(root->left); cout<<root->val<<" "; inorder(root->right); } } void postorder(TreeNode *root) { if(root) { postorder(root->left); postorder(root->right); cout<<root->val<<" "; } }};int main(){ vector<int> preorder={1,2,4,5,3,6,7}; vector<int> inorder={4,2,5,1,6,3,7}; Solution s; TreeNode *root=s.buildTree(preorder,inorder); s.preorder(root); cout<<endl; s.inorder(root); cout<<endl; s.postorder(root); cout<<endl;}
运行结果:
出现:
Status:
Memory Limit Exceeded
Submitted: 37 minutes ago
C++代码:
#include<iostream>#include<new>#include<vector>using namespace std;//Definition for binary treestruct TreeNode{ int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {public: TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { if(preorder.empty()||inorder.empty()) return NULL; TreeNode *root=new TreeNode(preorder[0]); int i; for(i=0;i<(int)inorder.size();i++) { if(preorder[0]==inorder[i]) break; } if(i>=(int)inorder.size()) return NULL; vector<int> ileft(inorder.begin(),inorder.begin()+i); vector<int> iright(inorder.begin()+i+1,inorder.end()); vector<int> pleft(preorder.begin()+1,preorder.begin()+1+i); vector<int> pright(preorder.begin()+1+i,preorder.end()); root->left=buildTree(pleft,ileft); root->right=buildTree(pright,iright); return root; } void preorder(TreeNode *root) { if(root) { cout<<root->val<<" "; preorder(root->left); preorder(root->right); } } void inorder(TreeNode *root) { if(root) { inorder(root->left); cout<<root->val<<" "; inorder(root->right); } } void postorder(TreeNode *root) { if(root) { postorder(root->left); postorder(root->right); cout<<root->val<<" "; } }};int main(){ vector<int> preorder={1,2,4,5,3,6,7}; vector<int> inorder={4,2,5,1,6,3,7}; Solution s; TreeNode *root=s.buildTree(preorder,inorder); s.preorder(root); cout<<endl; s.inorder(root); cout<<endl; s.postorder(root); cout<<endl;}
主要是重新分配了新的vector,出现内存超过限制。
Construct Binary Tree from Preorder and Inorder Traversal
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。