(二叉树 递归) 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< ...
随机推荐
- 关于MongoDB数据库的日志解析
MongoDB日志记录了数据库实例的健康状态.语句的执行状况.资源的消耗情况,所以日志对于分析数据库服务和性能优化很有帮助. 因此,很有必要花费一些时间来学习解析一下MongoDB的日志文件. 日志信 ...
- 【导航】Python常用资源(从新手到大牛)
[博客导航] [Python相关] 个人网站/博客/学习平台 中国大学MOOC :高校课程网上公开课学习平台,<Python语言程序设计>是我的入门课. Python123平台 :跟中国 ...
- Linux Collection:网络配置
PAS 缺少ifconfig 安装相应软件[不推荐],尽量使用 ip 命令 sudo apt install gnome-nettool 补充,显示IP地址: ip show address PAS ...
- 云数据库PolarDB(一)
一.出现的背景及PolarDB简介 阿里云,中国第一家拥有完整云计算能力的企业. 2015年,在计算界的奥运会Sort Benchmark中,阿里云计算100TB数据排序只用了不到7分钟,把Apach ...
- js倒计时、计时开始
最近项目中用到倒计时与计时的功能,代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8& ...
- 用CMD打开chrome并导航到百度(golang)
首选在cmd中输入(注意:根据你的电脑路径修改,可能是Progra~1): C:\Progra~\Google\Chrome\Application\chrome.exe www.baidu.com ...
- token
18f9nWvThC274lo3USjgfeldynt0t/r/w0yjLbj9 http://app-static.acc5.com/app/testpost.php
- 《Linux/UNIX系统编程手册》第63章 IO多路复用、信号驱动IO以及epoll
关键词:fasync_helper.kill_async.sigsuspend.sigaction.fcntl.F_SETOWN_EX.F_SETSIG.select().poll().poll_wa ...
- 玩转PIL库
1.安装pillow库: 在cmd下,输入简单的命令: pip install pillow 即可安装pillow库. 2.PIL库的简介: 1. PIL库主要有2个方面的功能: (1) 图像归档: ...
- 炸弹人游戏开发系列(7):加入敌人,使用A*算法寻路
前言 上文中我们实现了炸弹人与墙的碰撞检测,以及设置移动步长来解决发现的问题.本文会加入1个AI敌人,敌人使用A*算法追踪炸弹人. 本文目的 加入敌人,追踪炸弹人 本文主要内容 开发策略 加入敌人 实 ...