105. Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:淘宝店铺韩版女装货源 编辑:程序博客网 时间:2024/05/22 13:56
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; }};
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; }};
0 0
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
- [Leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode --- 105. Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]*105.Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode] 105.Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal LeetCode
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode#105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- leetcode---Simplify Path---栈
- Markdown笔记
- 数塔python
- 不要在该约炮的年龄谈修行------杨绛与你我无关(写得太他妈好了)
- Linux内核编程六:进程管理
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- ConcurrentHashMap如何实现高并发性的
- 利用DOM进行评论的发表与删除
- ubuntu下安装mysql及卸载mysql方法
- 从零开始配置Ubuntu Server 14.04 图形界面、Caffe安装及远程连接
- gson和bson4jackson 的简单记录
- mysql 笔记分享
- Settings.db在android6.0上的变化
- qt中绘图事件paintEvent 的用法