Leetcode-Construct Binary Tree from inorder and postorder travesal
Given inorder and postorder traversal of a tree, construct the binary tree.
Solution:
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
if (inorder.length==0)
return null; int len = inorder.length;
TreeNode root = buildTreeRecur(inorder,postorder,0,len-1,0,len-1);
return root;
} //Build tree for current list, i.e., inorder[inHead] to inorder[inEnd].
public TreeNode buildTreeRecur(int[] inorder, int[] postorder, int inHead, int inEnd, int postHead, int postEnd){
if (inHead==inEnd){
TreeNode root = new TreeNode(inorder[inHead]);
return root;
} int curRoot = postorder[postEnd];
int index = -1;
for (int i=inHead;i<=inEnd;i++)
if (inorder[i]==curRoot){
index = i;
break;
}
int leftNodeNum = index-inHead; int leftInHead = inHead;
int leftInEnd = inHead+leftNodeNum-1;
int rightInHead = index+1;
int rightInEnd = inEnd; int leftPostHead = postHead;
int leftPostEnd = postHead+leftNodeNum-1;
int rightPostHead = leftPostEnd+1;
int rightPostEnd = postEnd-1; TreeNode root = new TreeNode(curRoot);
TreeNode leftChild = null;
if (leftInEnd>=inHead){
leftChild = buildTreeRecur(inorder,postorder,leftInHead,leftInEnd,leftPostHead,leftPostEnd);
root.left = leftChild;
} TreeNode rightChild = null;
if (rightInHead<=inEnd){
rightChild = buildTreeRecur(inorder,postorder,rightInHead,rightInEnd,rightPostHead,rightPostEnd);
root.right = rightChild;
} return root;
}
}
We need to be very carefull about how to count the start and end of the left sub-tree and the right-sub tree. Especially detecting the case that some sub-tree is void.
A better way is to calculate the number of nodes in left and right tree first, then find out the range, like this:
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
if (inorder.length==0)
return null; int len = inorder.length;
TreeNode root = buildTreeRecur(inorder,postorder,0,len-1,0,len-1);
return root;
} //Build tree for current list, i.e., inorder[inHead] to inorder[inEnd].
public TreeNode buildTreeRecur(int[] inorder, int[] postorder, int inHead, int inEnd, int postHead, int postEnd){
if (inHead==inEnd){
TreeNode root = new TreeNode(inorder[inHead]);
return root;
} int curRoot = postorder[postEnd];
TreeNode root = new TreeNode(curRoot);
TreeNode leftChild = null;
TreeNode rightChild = null; int index = -1;
for (int i=inHead;i<=inEnd;i++)
if (inorder[i]==curRoot){
index = i;
break;
}
int leftNodeNum = index-inHead;
int rightNodeNum = inEnd-index; if (leftNodeNum>0){
int leftInHead = inHead;
int leftInEnd = inHead+leftNodeNum-1;
int leftPostHead = postHead;
int leftPostEnd = postHead+leftNodeNum-1;
leftChild = buildTreeRecur(inorder,postorder,leftInHead,leftInEnd,leftPostHead,leftPostEnd);
root.left = leftChild;
} if (rightNodeNum>0){
int rightInHead = index+1;
int rightInEnd = inEnd;
int rightPostHead = postEnd-rightNodeNum;
int rightPostEnd = postEnd-1;
rightChild = buildTreeRecur(inorder,postorder,rightInHead,rightInEnd,rightPostHead,rightPostEnd);
root.right = rightChild;
} return root;
}
}
Leetcode-Construct Binary Tree from inorder and postorder travesal的更多相关文章
- [Leetcode] Construct binary tree from inorder and postorder travesal 利用中序和后续遍历构造二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume th ...
- 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 ...
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- Leetcode Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal @ Python
原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意: ...
- LeetCode——Construct Binary Tree from Inorder and Postorder Traversal
Question Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may a ...
- [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
随机推荐
- node.js express安装问题
Windows下安装npm和express 1.如果不能在线安装,可以下载npm的zip解压到本地,然后把下面的bin目录加入到path中. 2.搞定npm后,执行了npm install expre ...
- 【iOS开发-33】学习手动内存管理临时抛弃ARC以及retain/assign知识——iOSproject师面试必考内容
我们为什么须要内存管理?当使用内存达到40M和45M时候会发出警告,假设不处理,占用内存达到120M时直接强制关闭程序. 所以出现闪退除了是程序出现逻辑错误,还有可能是内存使用过大. (1)创建一个对 ...
- 【学习笔记】C#中的单元测试
周一老师讲完单元测试以后,感觉挺好玩,通过查资料和相关书籍学到了几种C#在VS2010的测试方法,跟大家分享下,图文并茂啊,有木有~~ 1.从被测试的代码中生成单元测试 1.1创建C#控制台程序,命名 ...
- signed distance field 算法
将二值图转化成signed distance field后,可以在双线性插值下实现平滑放大. 定义: 到前景的distance field:各点到最近前景点的距离. 到背景的distance fiel ...
- C语言 文件操作
/** *@author cody *@date 2014-08-09 *@description copy text file * FILE *fopen(filename,openmode) * ...
- SecureCRT终端上使用spark-shell时按退格键无反应的解决方法
问题:用SecureCRT远程连接至Spark集群,启动spark-shell却发现输错命令后却无法用退格键删除. 解决方法: 第一步: 在SecureCRT的菜单栏选择“OPtions(选项)”按钮 ...
- [转]鼠标和键盘模拟API
几乎所有的游戏中都使用了鼠标来改变角色的位置和方向,玩家仅用一个小小的鼠标,就可以使角色畅游天下. 那么,我们如何实现在没有玩家的参与下角色也可以自动行走呢.其实实现这个并不难,仅仅几个Windows ...
- APK反编译之一:基础知识
作者:lpohvbe | http://blog.csdn.net/lpohvbe/article/details/7981386 这部分涉及的内容比较多,我会尽量从最基础开始说起,但需要读者一定的a ...
- python-hanoi
#!/usr/bin/env python #-*- coding:utf-8 -*- ############################ #File Name: hanoi.py #Autho ...
- 0045 Spring中使用DataSourceTransactionManager进行事务管理的xml配置
在一个业务的实现过程中,可能需要多条sql完成对数据库的操作,比如账户登录,需要匹配用户名和密码,然后要增加积分,还要记录登录的ip和时间,这可能需要三个sql语句,这三个语句应当是一个整体,任意一个 ...