105. Construct Binary Tree from Preorder and Inorder Traversal (Tree; DFS)
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
class Solution {
public:
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
root = NULL;
if(inorder.empty()) return root;
root = new TreeNode();
buildSubTree(preorder,inorder,,preorder.size()-,,inorder.size()-,root);
return root;
}
void buildSubTree(vector<int> &preorder, vector<int> &inorder, int preStartPos, int preEndPos,int inStartPos, int inEndPos, TreeNode * currentNode) //对于树的递归遍历,如果涉及数组,必定参数中要传递start,end,指明当前子树来源于数组的哪一部分
{
currentNode->val = preorder[preStartPos]; //前序遍历的第一个为根节点
//find root position in inorder vector
int inRootPos;
for(int i = inStartPos; i <= inEndPos; i++)
{
if(inorder[i] == preorder[preStartPos])
{
inRootPos = i;
break;
}
}
//分别递归处理左子树和右子树
//left tree:是总序遍历中根节点所在位置之前的部分,对应前序遍历根节点之后相同长度的部分
int newPrePos = preStartPos+max(inRootPos-inStartPos, );
if(inRootPos>inStartPos)
{
currentNode->left = new TreeNode();
buildSubTree(preorder,inorder,preStartPos+, newPrePos,inStartPos,inRootPos-,currentNode->left); //递归调用时,要传递新的start,end
}
//right Tree:是中序遍历根节点所在位置之后的一部分,对应前序遍历从末尾开始相同的长度
if(inRootPos < inEndPos)
{
currentNode->right = new TreeNode();
buildSubTree(preorder,inorder,newPrePos+, preEndPos,inRootPos+,inEndPos,currentNode->right); //递归调用时,要传递新的start,end
}
}
private:
TreeNode* root;
};
105. Construct Binary Tree from Preorder and Inorder Traversal (Tree; DFS)的更多相关文章
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- 【题解二连发】Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal
LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- LeetCode_Construct Binary Tree from Preorder and Inorder Traversal
一.题目 Construct Binary Tree from Preorder and Inorder Traversal My Submissions Given preorder and ino ...
- [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 ----- java
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
随机推荐
- java并发编程之一--Semaphore的使用
1.介绍 Semaphore 中文的含义 信号,信号系统,此类的只要作用就是限制线程的并发的数量. Semaphore内部主要通过AQS(AbstractQueuedSynchronizer)实现线程 ...
- I.MX6 PWM buzzer driver hacking with Demo test
/***************************************************************************** * I.MX6 PWM buzzer dr ...
- bug 报告
准备工作:首先我下载了Git Bash这个软件,利用了老师所给的git命令,批量的pull所有同学第二次作业的代码.截图如下: 一.被测试程序的作者:方铭 程序git地址:https://coding ...
- cratedb geo 查询
cratedb支持的geo 查询还相对比较全,开发基本的功能已经够用了 安装cratedb 使用docker docker run -d -p 4200:4200 crate 创建数据库 创建表 CR ...
- 使用docker 部署graylog集群
graylog 相比elk 有比较简单的方面,使用简单,配置简单,可视化工具是一体化的,比较方便 搭建使用docker,多主机部分,结合docker-compose 进行管理 具体docker 配置文 ...
- IT项目管理的十六个字心得体会
目标驱动,系统思维,风险意识,数据量化 凡事预则立,不预则废.如果你不知道要到哪里?给你一张地图也没有用.目标驱动首先要有最基本的计划管理和时间管理能力.对于一个项目,我们过程中做的所有工作都是为了要 ...
- ffmpeg V4L2_BUF_FLAG_ERROR的解决方法
利用ffmpeg进行视频采集时经常出现“V4L2_BUF_FLAG_ERROR”的错误,并不再进行下帧的采集.通过借鉴下面的方法,对ffmpeg3.0.7版本进行补丁,能解决此类问题. 当某帧出错后, ...
- Sql Server中的事务隔离级别
数据库中的事物有ACID(原子性,一致性,隔离性,持久性)四个特性.其中隔离性是用来处理并发执行的事务之间的数据访问控制.SqlServer中提供了几种不同级别的隔离类型. 概念 Read UnCom ...
- 如何查看oracle表空间是否自动扩展
select file_name,autoextensible,increment_by from dba_data_files
- java 面向对象 — 继承
继承中的构造方法,先执行父类中的构造方法,然后执行子类中的构造方法 继承中的属性,最后执行的属性 覆盖前面的属性 因为是开辟了 两个内存空间,所以相比较是不同的. 如果想比较两个对象的值是否相同的话, ...