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 ...
随机推荐
- 使用jq Deferred防止代码被回调函数分解分解的支离破碎
//移动人物 function moveInterval(stopPosotion){ var dtd = $.Deferred(); // 生成Deferred对象 var yidong= wind ...
- Eclipse - JAR包制作
Eclipse - JAR包制作细节 1.Jar包分为两种,一种是不可运行的,一种是可运行的Jar包,他们的主要区别如下: > 不可直接运行的Jar包主要是用于给别的程序提供调用 ...
- Firefly 3288又一次制作android和lubuntu双系统固件
又一次制作android和lubuntu双系统固件 因为本人改动了lubuntu的驱动和设备树信息,为了方便烧写系统,所以又一次制作了双系统的固件. Firefly wiki教程里有android固件 ...
- Json数组操作小记 及 JSON对象和字符串之间的相互转换
[{"productid":"1","sortindex":"2"},{"productid":&q ...
- FreeSWITCH技巧:如何向通话的另一方号码发送dtmf?
注:这里的文章都是本人的日常总结,请尊重下个人的劳动成果,转载的童鞋请注明出处,谢谢. 如您转载的文章发生格式错乱等问题而影响阅读,可与本人联系,无偿提供本文的markdown源代码. 联系邮箱:ji ...
- Atitit .h5文件上传
Atitit .h5文件上传 1. 上传原理1 2. Html1 3. Js2 4. uploadV2.js2 5. upServlet & FileUploadService {3 6. 注 ...
- flink checkpoint 源码分析 (二)
转发请注明原创地址http://www.cnblogs.com/dongxiao-yang/p/8260370.html flink checkpoint 源码分析 (一)一文主要讲述了在JobMan ...
- ubuntu MySQL数据库输入中文乱码 解决方案
一.登录MySQL查看用SHOW VARIABLES LIKE ‘character%’;下字符集,显示如下:+--------------------------+----------------- ...
- CSS学习笔记(1)--浮动
总结:浮动只能在脱离文档流的当前位置向上浮动,不能像定位一样到处乱跑. 清除浮动,设置一个类.clear{clear:both;} 1.没有浮动,都独占一行: <!DOCTYPE html> ...
- 构建自己的embedded linux系统
[教程]使用buildroot完全自定义自己的embedded linux系统(nand)http://www.eeboard.com/bbs/thread-38377-1-1.html [教程] [ ...