105. Construct Binary Tree from Preorder and Inorder Traversal
2016-05-27 22:46
375 查看
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
递归
/**
* 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* buildTree(vector<int>& preorder, vector<int>& inorder) {
return Create(preorder, 0, preorder.size(),inorder,0, inorder.size());
}
TreeNode* Create(vector<int>& preorder, int pre_i,int pre_j,vector<int>& inorder,int in_i,int in_j)
{
if(pre_i>=pre_j||in_i>=in_j) return NULL;
int mid=preorder[pre_i];
auto mid_i=find(inorder.begin()+in_i,inorder.begin()+in_j,mid);
int dis=mid_i-inorder.begin()-in_i;
TreeNode* root=new TreeNode(mid);
root->left=Create(preorder, pre_i+1,pre_i+1+dis,inorder,in_i,in_i+1+dis);
root->right=Create(preorder, pre_i+1+dis,pre_j,inorder,in_i+1+dis,in_j);
return root;
}
};
Note:
You may assume that duplicates do not exist in the tree.
递归
/**
* 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* buildTree(vector<int>& preorder, vector<int>& inorder) {
return Create(preorder, 0, preorder.size(),inorder,0, inorder.size());
}
TreeNode* Create(vector<int>& preorder, int pre_i,int pre_j,vector<int>& inorder,int in_i,int in_j)
{
if(pre_i>=pre_j||in_i>=in_j) return NULL;
int mid=preorder[pre_i];
auto mid_i=find(inorder.begin()+in_i,inorder.begin()+in_j,mid);
int dis=mid_i-inorder.begin()-in_i;
TreeNode* root=new TreeNode(mid);
root->left=Create(preorder, pre_i+1,pre_i+1+dis,inorder,in_i,in_i+1+dis);
root->right=Create(preorder, pre_i+1+dis,pre_j,inorder,in_i+1+dis,in_j);
return root;
}
};
O(n)(19ms) solution without recusion.
/** * 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* buildTree(vector<int>& preorder, vector<int>& inorder) { TreeNode *root=NULL; stack<TreeNode *> MyData; if(preorder.empty()) return root; root = new TreeNode(preorder[0]); MyData.push(root); int index = 0; for(int i=1; i<=preorder.size(); i++) { TreeNode *cur = MyData.top(); if((MyData.top()->val)!=inorder[index]) { cur->left = new TreeNode(preorder[i]); MyData.push(cur->left); } else { while(!MyData.empty() && ((MyData.top()->val)==inorder[index])) { cur=MyData.top(); MyData.pop(); index++; } if(index<inorder.size()) { cur->right = new TreeNode(preorder[i]); MyData.push(cur->right); } } } return root; } };
相关文章推荐
- MYSQL的binary解决mysql数据大小写敏感问题的方法
- 处理使用MariaDB做级联复制遇到的error 1666
- ThinkPHP中__initialize()和类的构造函数__construct()用法分析
- iOS上传二进制流文件,PHP接收并生成文件
- 排序binary tree
- eclipse C/C++ 问题(launch failed,binary not found)
- launch failed.Binary not found in Linux/Ubuntu解决方案
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal
- Binary Tree Zigzag Level Order Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- Flatten Binary Tree to Linked List
- Populating Next Right Pointers in Each Node II
- Gray Code
- Add Binary @ LeetCode java
- 005-LAMP_MySQL binary installation
- Mysql查询大小写不敏感简便解决办法
- Add Binary
- cf.256.div2.D