【LeetCode】105 & 106 Construct Binary Tree from (Preorder and Inorder) || (Inorder and Postorder)Traversal
Description:
Given arrays recording 'Preorder and Inorder' Traversal (Problem 105) or 'Inorder and Postorder' (Problem 106), u need build the binary tree.
Input:
105. Preorder & Inorder traversal
106. Inorder & Postorder traversal
output:
A binary tree.
Solution:
This solution uses the algorithm "divide and conquer". Taking an example of 105, we can get the root node from preorder travelsal then use inorder traversal to divide the range of left tree nodes and the right tree nodes. You can
draw some simple instances on paper, which will make you totally clear about the thinking.
/**
* 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:
void travel(TreeNode **root)
{
if(*root){
travel(& ((*root) -> left));
cout<<"val is "<<(*root)->val<<endl;
travel(& ((*root) -> right));
}
} TreeNode* createTree(vector<int>& preorder, vector<int>& inorder, int preSta, int preEnd, int inSta, int inEnd)
{
if(preSta > preEnd || inSta > inEnd ) return NULL;
TreeNode *root = new TreeNode(preorder[preSta]);
int index;
for(int i = inSta; i <= inEnd; i ++){
if(inorder[i] == preorder[preSta]){
index = i;
break;
}
}
root -> left = createTree(preorder, inorder, preSta + , preSta + index - inSta, inSta, index - );
root -> right = createTree(preorder, inorder, preSta + index - inSta + , preEnd, index + , inEnd);
return root;
} TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
TreeNode *root = createTree(preorder, inorder, , preorder.size() - , , inorder.size() - );
TreeNode **r = &root;
//travel(r);
return root;
}
}; //106.
class Solution {
public:
TreeNode* createTree(vector<int>& inorder, vector<int>& postorder, int inSta, int inEnd, int postSta, int postEnd){
if(inSta > inEnd || postSta > postEnd)
return NULL;
TreeNode* root = new TreeNode(postorder[postEnd]);
int index;
for(int i = inEnd; i >= inSta; i --){
if(postorder[postEnd] == inorder[i]){
index = i;
break;
}
}
root -> right = createTree(inorder, postorder, index + , inEnd, postEnd - (inEnd - index), postEnd - );
root -> left = createTree(inorder, postorder, inSta, index - , postSta, postSta + (index - inSta - ));
return root;
}
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
TreeNode* root = createTree(inorder, postorder, , inorder.size() - , , postorder.size() - );
return root;
}
};
【LeetCode】105 & 106 Construct Binary Tree from (Preorder and Inorder) || (Inorder and Postorder)Traversal的更多相关文章
- 【LeetCode】105 & 106. Construct Binary Tree from Inorder and Postorder Traversal
题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume ...
- 105 + 106. Construct Binary Tree from Preorder and Inorder Traversal (building trees)
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 t ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)
这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...
- 【LeetCode】993. Cousins in Binary Tree 解题报告(C++ & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 【LeetCode】543. Diameter of Binary Tree 解题报告 (C++&Java&Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【leetcode】Minimum Depth of Binary Tree
题目简述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...
- 【leetcode】Minimum Depth of Binary Tree (easy)
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
随机推荐
- PAT 1137 Final Grading
For a student taking the online course "Data Structures" on China University MOOC (http:// ...
- 从“菜鸟”码农到“资深”架构师,我到底经历了什么?--------http://baijiahao.baidu.com/s?id=1585813883835208757&wfr=spider&for=pc
http://baijiahao.baidu.com/s?id=1585813883835208757&wfr=spider&for=pc
- 重载与重写的区别----https://blog.csdn.net/zhu_apollo/article/details/1852542
重载 overloading 1) 方法重载是让类以统一的方式处理不同类型数据的一种手段.多个同名函数同时存在,具有不同的参数个数/类型.重载是一个类中多态性的一种表现. ...
- [洛谷P1114] “非常男女”计划
题目描述 近来,初一年的XXX小朋友致力于研究班上同学的配对问题(别想太多,仅是舞伴),通过各种推理和实验,他掌握了大量的实战经验.例如,据他观察,身高相近的人似乎比较合得来. 万圣节来临之际,XXX ...
- iptables中增加/删除/查询/修改的基本操作
虽然在Ubuntu使用了UFW来简化iptables的操作,但是UFW只针对防火墙方面,转发方面没有涉及,所以要弄懂其中的原理,还是必须回归到iptables中.CentOS也是如此.下面是针对ipt ...
- 浅谈SQL Server 对于内存的管理--宋沄剑 英文名:CareySo
http://www.cnblogs.com/CareySon/archive/2012/08/16/HowSQLServerManageMemory.html
- Android GIS开发系列-- 入门季(3) GraphicsLayer添加点、线、面
GraphicsLayer是图形图层,可以自定义图形添加到地图上.调用GraphicsLayer的addGraphic方法就能添加图形,此方法要一个Graphic对象,此对象的构造方法是Graphic ...
- cc2540 cc2541 开发板资料更新日志
经过多次PCB打样和全面调试.已经完毕了cc2540 cc2541的开发板的批量贴片.硬件告一段落, 接下来是全面完好软件方面的工作.眼下已经针对没有开发经验的用户编写完毕0基础基础实验代码和教程.接 ...
- kettle_删除“共享输出表”引发的错误
原创作品.出自 "深蓝的blog" 博客.欢迎转载,转载时请务必注明出处,否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlong ...
- Apache 配置学习占位
http://www.cnblogs.com/yeer/archive/2011/01/18/1938024.html http://www.cnblogs.com/zgx/archive/2011/ ...