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服务器
本文档介绍腾讯云·万象优图服务端nodejs的部署和集成,搭建一个nodejs+nginx为基础,对web端或者移动端提供http签名接口服务的例子程序.注意:本文档只是简单的示例,展示了服务端为终端 ...
- Android中Context的总结及其用法
在android中我们经常遇到这样的情况,在创建一个对象的时候往往需要传递一个this参数,比如:语句 MyView mView = new MyView(this),要求传递一个this参数,这个t ...
- ArcGIS教程:将“替换为模型”工具用于多面体
替换为模型工具出如今 3D 编辑器 工具条上的 3D 编辑器菜单中.而且仅仅适用于多面体要素.使用此命令可将所选的一个或多个要素的几何替换为磁盘中所保存的 3D 模型文件.受支持的 3D 模型类型包含 ...
- linux下Oracle数据库实例开机自启动设置
linux下数据库实例开机自启动设置 1.改动/oratab [root@org54 ~]# vi/etc/oratab --把N改为Y,例如以下提示 # This file is used ...
- php 36进制与10进制转换
php 36进制与10进制转换 /** * @desc im:十进制数转换成三十六机制数 * @param (int)$num 十进制数 * return 返回:三十六进制数 */ function ...
- JVM-String常量池与运行时常量池
Start with JVM 周志明先生著-<深入理解Java虚拟机>,书买回来好几天了,但是最近才准备开始搞一搞了(哭瞎…..).首先是第一章的Java以及JVM发展历史,大概知道了现行 ...
- AES加密在windows与linux平台下显示结果不同,解决方案
现象描述: 在 windows 操作系统下加解密正常,但部署到 linux 环境中相同的输入加密结果不正确,并且每次运行返回的结果都不同.也就是说在windows下加解密都正常,一但部署到linux下 ...
- JavaScript变量的生命周期
最近看国外经典教材的时候发现JavaScript与熟知的Java,C,C++都不同的特性,其中一个就是变量的生命周期. 1.在JavaScript中,对于for循环中定义的i变量,其生命周期在循环 ...
- javascript 函数声明和函数表达式
定义js函数的方法有两种,1.函数声明 2.函数表达式 这两种方式的区别是:1.函数声明可以先调用后定义(javascript引擎在解释的时候会把所有的函数声明提升)2.函数表达式必须先定义后使用.看 ...
- 删除Win10的自带应用
显示 Get-AppxPackage | Select Name, PackageFullName 按关键字删除 Get-AppxPackage *camera* | Remove-AppxPacka ...