(二叉树 递归) 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< ...
随机推荐
- LeetCode算法题-Relative Ranks(Java实现)
这是悦乐书的第248次更新,第261篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第115题(顺位题号是506).根据N名运动员的得分,找到他们的相对等级和得分最高的三个 ...
- MySQL 数据表创建及管理
use stuinfo; -- 指定当前数据库 CREATE table if not exists student1( -- 创建数据表student1 sNo ) not NULL, sName ...
- ES6 快速入门
ES6 初识 ES6 是 ECMAScript 6.0 的简写,即 JavaScript 语言的下一代标准,已经在 2015年6月正式发布了,它的目标是让JS能够方便的开发企业级大型应用程序,因此,E ...
- Eclipse编写代码时设置属于自己的注释
翻看硬盘文件,偶然发现以前存的这么一个小操作,给大家分享一下 1.打开Eclipse,按照以下步骤进行设置: Window -->Preferences->Java->Editor- ...
- syso快捷键设置
syso快捷键
- laravel学习笔记一
指定端口 数据迁移 php artisan migrate:install 任何路由 match get,post只选择其一 没有表名对应默认的posts表,如果表为post就不行 时区不对时 分页 ...
- 由Redis的hGetAll函数所引发的一次服务宕机事件
昨晚通宵生产压测,终于算是将生产服务宕机的原因定位到了,心累.这篇博客,算作一个复盘和记录吧... 先来看看Redis的缓存淘汰算法思维导图: 说明:当实际占用的内存超过Redis配置的maxmemo ...
- Linux内存管理 (25)内存sysfs节点解读
1. General 1.1 /proc/meminfo /proc/meminfo是了解Linux系统内存使用状况主要接口,也是free等命令的数据来源. 下面是cat /proc/meminfo的 ...
- SpringBoot 数据篇之使用JDBC
SpringBootTutorial :: Data :: Jdbc 简介 API execute update query 实战 配置数据源 完整示例 引申和引用 简介 Spring Data 包含 ...
- 爬取页面InsecureRequestWarning: 警告解决笔记
InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is s ...