[LeetCode-21]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.
_______7______
/ \
__10__ ___2
/ \ /
4 3 _8
\ /
1 11
preorder = {7,10,4,3,1,2,8,11}
inorder = {4,10,3,1,7,11,8,2}
The first node in preorder alwasy the root of the tree. We can break the tree like:
1st round:
preorder: {7}, {10,4,3,1}, {2,8,11}
inorder: {4,10,3,1}, {7}, {11, 8,2}
_______7______
/ \
{4,10,3,1} {11,8,2}
part agin based on the preorder.
2nd round
left part right part
preorder: {10}, {4}, {3,1} {2}, {8,11}
inorder: {4}, {10}, {3,1} {11,8}, {2}
_______7______
/ \
__10__ ___2
/ \ /
4 {3,1} {11,8}
Same way to split {3,1} and {11,8}, yo will get the complete tree now.
_______7______
/ \
__10__ ___2
/ \ /
4 3 _8
\ /
1 11
c++
TreeNode *BuildTreePI(
vector<int> &preorder,
vector<int> &inorder,
int p_s, int p_e,
int i_s, int i_e){
if(p_s > p_e) return NULL;
int pivot = preorder[p_s];
int i = i_s;
for(;i<i_e;i++){
if(inorder[i] == pivot)
break;
}
int length1 = i-i_s-1;
int length2 = i_e-i-1;
TreeNode* node = new TreeNode(pivot);
node->left = BuildTreePI(preorder,inorder,p_s+1,length1+p_s+1,i_s, i-1);
node->right = BuildTreePI(preorder, inorder, p_e-length2, p_e, i+1, i_e);
return node;
}
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
return BuildTreePI(preorder,inorder,0,preorder.size()-1,0,inorder.size()-1);
}
java
public TreeNode buildTree(int[] preorder, int[] inorder) {
return buildPI(preorder, inorder, 0, preorder.length-1, 0, inorder.length-1);
}
public TreeNode buildPI(int[] preorder, int[] inorder, int p_s, int p_e, int i_s, int i_e){
if(p_s>p_e)
return null;
int pivot = preorder[p_s];
int i = i_s;
for(;i<i_e;i++){
if(inorder[i]==pivot)
break;
}
TreeNode node = new TreeNode(pivot);
int lenLeft = i-i_s;
node.left = buildPI(preorder, inorder, p_s+1, p_s+lenLeft, i_s, i-1);
node.right = buildPI(preorder, inorder, p_s+lenLeft+1, p_e, i+1, i_e);
return node;
}[LeetCode-21]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 (用先序和中序树遍历来建立二叉树)
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 ...
- 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题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)
题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume t ...
- 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】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 (Medium)
原题 题意: 根据先序和中序得到二叉树(假设无重复数字) 思路: 先手写一次转换过程,得到思路. 即从先序中遍历每个元素,(创建一个全局索引,指向当前遍历到的元素)在中序中找到该元素作为当前的root ...
- Leetcode#105 Construct Binary Tree from Preorder and Inorder Traversal
原题地址 基本二叉树操作. O[ ][ ] [ ]O[ ] 代码: TreeNode *restore(vector< ...
随机推荐
- Centos7 卸载rpm包、卸载yum安装的包
1. 通过rpm -q <关键字>查到rpm包的名字.2. 调用rpm -e <包名>删除特定的rpm包
- PyCharm 环境配置
1.去掉“自动保存功能” pycharm默认是自动保存的,习惯自己按 ctrl + s 的可以进行如下设置: 菜单File -> Settings... -> Appearance &am ...
- 没有博士学位,照样玩转TensorFlow深度学习
教程 | 没有博士学位,照样玩转TensorFlow深度学习 机器之心2017-01-24 12:32:22 程序设计 谷歌 操作系统 阅读(362)评论(0) 选自Codelabs 机器之心编译 参 ...
- obj-fit
该 object-fit CSS 属性指定替换元素的内容应该如何适应到其使用的高度和宽度确定的框. 初始值 fill 适用于 替换元素 遗传 是 媒体 视觉 计算值 按规定 动画类型 离散的 为了规范 ...
- There's no Qt version assigned to this project for platform Win32. Please use the 'change Qt version' feature and choose a valid Qt version for this platform.
这个是用在vs2015时爆出的问题. 解决方法是: 1.鼠标放置在解决方案中的工程名处,用鼠标右键点击(右击). 2.选择Qt Project Settings,在弹出的对话框中的version栏处填 ...
- ubi实际使用
ubifs号称性能比yaffs2 好,同时压缩可读写,文件系统image体较小同时可写.1. uboot使能对UBIFS的支持#define CONFIG_CMD_NAND#define CONFIG ...
- Elasticsearch5.X IN Windows 10 系列文章(3)
系统类型:windows10 64位家庭版 ElasticSearch版本: 5.5.1 (最新稳定版为5.5.2),由于用到IK中文分词插件,最新版本没有5.5.2 ,所以使用5.5.1 日期:20 ...
- python第四周迭代器生成器序列化面向过程递归
第一节装饰器复习和知识储备------------ 第一节装饰器复习和知识储备------------ def wrapper(*args,**kwargs): index(*args,**kwa ...
- [前端开发] 未来趋势“无后端”的web应用开发模式
最近看到前端趋势2013大会上的一篇文章,题目是<各位快看,不用后端>,觉得有点意思,恰好近期的一次讨论及半年前的一次开发实践也涉及到这种模式,简单谈谈我的想法. 不得不说,文章的题目确实 ...
- 【翻译自mos文章】当点击完 finishbutton后,dbca 或者dbua hang住
当点击完 finishbutton后,dbca 或者dbua hang住 来源于: DBCA/DBUA APPEARS TO HANG AFTER CLICKING FINISH BUTTON (文档 ...