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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  4. [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 ...

  5. 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 ...

  6. [leetcode]Construct Binary Tree from Inorder and Postorder Traversal @ Python

    原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意: ...

  7. 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 ...

  8. [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...

  9. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

  10. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

随机推荐

  1. word2016打开2007文档出错

    不知从何时起,我电脑上的一部分office文件打开时频频报错!!! 有一段时间发现,通过iis发布的网站上下载excel文件的时候,通过localhost下载的文件能够正常打开,但是通过IP和端口下载 ...

  2. 阻塞IO、非阻塞IO的区别

    1.类与类之间的关系:依赖,实现,泛化(继承),关联,组合,聚合. 1)依赖(虚线):一个类是 另一个类的函数参数 或者 函数返回值. 2)实现(实线加小圆):对纯虚函数类(抽象类)的实现. 3)继承 ...

  3. Hadoop文件解压缩

    Class org.apache.hadoop.io.compress .CompressionCodecFactory A factory that will find the correct co ...

  4. poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分, dinic, isap

    poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分 dinic /* * Author: yew1eb * Created Time: 2014年10月31日 星期五 ...

  5. Mac上的学习神器:Marginnote

    https://marginnote.com/?lang=zh-hans 技巧1:合并 多选编辑 - 按顺序选择多个块 - 左下角菜单 - 合并 技巧2:管理顺序 双击图片并且按住不放,即可拖拽顺序 ...

  6. 捅伊朗黑客PP — 后台登陆POST+错误回显 注入

    看了一个泰国政府的网站被伊朗的黑客挂页,上面写着“Your Box 0wn3z By Behrooz_Ice – Q7x -Sha2ow -Virangar -Ali_Eagle -iman_takt ...

  7. uva 11400 - Lighting System Design(动态规划 最长上升子序列问题变型)

    本题难处好像是在于 能够把一些灯泡换成电压更高的灯泡以节省电源的钱 .所以也才有了对最优方案的探求 好的处理方法是依照电压从小到大排序.仅仅能让前面的换成后面的.也就满足了把一些灯泡换成电压更高的灯泡 ...

  8. 在 Linux 客户端配置基于 Kerberos 身份验证的 NFS 服务器

    在这篇文章中我们会介绍配置基于 Kerberos 身份验证的 NFS 共享的整个流程.假设你已经配置好了一个 NFS 服务器和一个客户端.如果还没有,可以参考 安装和配置 NFS 服务器[2] - 它 ...

  9. 文件上传之 MultipartFile

    利用MultipartFile(组件)实现文件上传 在java中上传文件似乎总有点麻烦,没.net那么简单,记得最开始的时候用smartUpload实现文件上传,最近在工作中使用spring的Mult ...

  10. 通过ip查看主机名和端口占用情况

      1. 知道对方ip查看对方的计算机名 方法:开始->运行->cmd->net view 对方ip 或者 开始->运行->cmd->nbtstat -a 对方ip ...