(二叉树 递归) leetcode 105. 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.
For example, given
preorder = [3,9,20,15,7]
inorder = [9,3,15,20,7]
Return the following binary tree:
3
/ \
9 20
/ \
15 7
-----------------------------------------------------------------------------------
就是从先序遍历和中序遍历构建踹个二叉树,可以用递归方式,注意递归的终止条件。
和leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal几乎一样。
参考博客:http://www.cnblogs.com/grandyang/p/4296500.html
C++代码:
/**
* 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 build(preorder,,preorder.size() - ,inorder,,inorder.size() - );
}
TreeNode* build(vector<int> &preorder,int pleft,int pright,vector<int> &inorder,int ileft,int iright){
if(pleft > pright || ileft > iright) return NULL; //递归的终止条件。
int i = ;
TreeNode *cur = new TreeNode(preorder[pleft]);
for(i = ileft; i < inorder.size(); i++){
if(inorder[i] == cur->val)
break;
}
cur->left = build(preorder,pleft + ,pleft + i - ileft,inorder,ileft,i-);
cur->right = build(preorder,pleft + i - ileft + ,pright,inorder,i+,iright);
return cur;
}
};
也有一个方法:
/**
* 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 build(preorder,inorder);
}
TreeNode* build(vector<int> &preorder,vector<int> &inorder){
if(preorder.size() == || inorder.size() == ) return NULL; //递归条件。当然,也还可以加上if(preorder.size() == 1 || inorder.size() == 1) return cur;这个递归条件。
int rootval = preorder[];
int i = ;
for(i = ; i < inorder.size(); i++){
if(inorder[i] == rootval)
break;
}
vector<int> inleft,inright,preleft,preright;
for(int k = ; k < i + ; k++)
preleft.push_back(preorder[k]);
for(int k = i + ; k < preorder.size(); k++){
preright.push_back(preorder[k]);
inright.push_back(inorder[k]);
}
for(int k = ; k < i; k++)
inleft.push_back(inorder[k]);
TreeNode *cur = new TreeNode(rootval);
cur->left = build(preleft,inleft);
cur->right = build(preright,inright);
return cur;
}
};
这两个方法从思想上是一样的,只不过代码的实现有所不同。
(二叉树 递归) leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章
- [LeetCode] 105. 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 ...
- LeetCode 105. 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 ...
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal ----- java
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- Java for LeetCode 105 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 ...
- (二叉树 递归) leetcode 889. Construct Binary Tree from Preorder and Postorder Traversal
Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...
- [leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal (Medium)
原题 题意: 根据先序和中序得到二叉树(假设无重复数字) 思路: 先手写一次转换过程,得到思路. 即从先序中遍历每个元素,(创建一个全局索引,指向当前遍历到的元素)在中序中找到该元素作为当前的root ...
- leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal,剑指offer 6 重建二叉树
不用迭代器的代码 class Solution { public: TreeNode* reConstructBinaryTree(vector<int> pre,vector<in ...
- Leetcode#105 Construct Binary Tree from Preorder and Inorder Traversal
原题地址 基本二叉树操作. O[ ][ ] [ ]O[ ] 代码: TreeNode *restore(vector< ...
随机推荐
- Windows Server 2012 NIC Teaming 网卡绑定介绍及注意事项
Windows Server 2012 NIC Teaming 网卡绑定介绍及注意事项 转载自:http://www.it165.net/os/html/201303/4799.html Window ...
- MySQL中Identifier Case Sensitivity
在MySQL当中,有可能遇到表名大小写敏感的问题.其实这个跟平台(操作系统)有关,也跟系统变量lower_case_table_names有关系.下面总结一下,有兴趣可以查看官方文档"Ide ...
- DWH中增量数据的抽取
1. Truncate-Load 全量加载 简单直观.不易出错,适合数据量不太大的操作 性能问题 2. Increamental-Load 只考虑新增.修改.删除的记录 良好的数据源设计(主要是 ...
- Docker,Docker Compose,Docker Swarm,Kubernetes之间的区别
Dcoker Docker 这个东西所扮演的角色,容易理解,它是一个容器引擎,也就是说实际上我们的容器最终是由Docker创建,运行在Docker中,其他相关的容器技术都是以Docker为基础,它是我 ...
- Cloudera Manager和CDH5.8离线安装
https://blog.csdn.net/zzq900503/article/details/52982828 简介 我们在上篇文章中已经了解了CDH,为了后续的学习,我们本章就来安装CDH5.8. ...
- syso快捷键设置
syso快捷键
- vscode 编写vue
开启保存时检查代码语法 安装 让配置生效 添加新配置 cnpm install mockjs -D
- dispatch_barrier_async--屏障是一个同步点
Discussion Calls to this function always return immediately after the block has been submitted and n ...
- OpenCV 与 OpenGL 的关系是什么?
OpenCV是 Open Source Computer Vision LibraryOpenGL是 Open Graphics LibraryOpenCV主要是提供图像处理和视频处理的基础算法库,还 ...
- Analyzing 'enq: HW - contention' Wait Event (Doc ID 740075.1)
Analyzing 'enq: HW - contention' Wait Event (Doc ID 740075.1) In this Document Symptoms Cause ...