Given inorder and postorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.


题解:如下图所示的一棵树:

        5

      /   \

    2      4

  /   \      \

1      3       6

中序遍历序列:1  2  3  5  4  6

后序遍历序列:1  3  2  6  4  5

后序遍历序列的最后一个元素就是当前根节点元素。首先想到的方法是递归,重要的是在先序和中序序列中分清楚哪一部分是左子树的,哪一部分是右子树的。

递归的过程如下图所示:其中in和po分别代表递归调用时传递给左子树的中序遍历序列和后序遍历序列。

以第一次递归为例,说明左右子树的中序和后序遍历序列如何求:

如上图所示,现在中序序列中找到根节点的位置(5),就能够知道左子树(绿色圈)和右子树(橙色圈)的中序遍历序列了。然后根据中序遍历的长度等于后序遍历的长度,计算出左右子树的后序遍历序列,递归调用构造树函数即可。

代码如下:

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int InorderIndex(int[] inorder,int key){
if(inorder == null || inorder.length == 0)
return -1; for(int i = 0;i < inorder.length;i++)
if(inorder[i] == key)
return i; return -1;
}
public TreeNode buildTreeRec(int[] inorder,int[] postorder,int instart,int inend,int postart,int poend){
if(instart > inend)
return null;
TreeNode root = new TreeNode(postorder[poend]);
int index = InorderIndex(inorder, root.val);
root.left = buildTreeRec(inorder, postorder, instart, index-1, postart, postart+index-instart-1);
root.right = buildTreeRec(inorder, postorder, index+1, inend, postart+index-instart, poend-1); return root;
}
public TreeNode buildTree(int[] inorder, int[] postorder) {
return buildTreeRec(inorder, postorder, 0, inorder.length-1, 0, postorder.length-1);
}
}

【leetcode】Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章

  1. 【Leetcode】【Medium】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 ...

  2. 【树】Construct Binary Tree from Inorder and Postorder Traversal

    题目: Given inorder and postorder traversal of a tree, construct the binary tree. 思路: 后序序列的最后一个元素就是树根, ...

  3. 【LeetCode106】Construct Binary Tree from Inorder and Postorder Traversal★★

    1.题目 2.思路 思路和LeetCode105类似,见上篇. 3.java代码 //测试 public class BuildTreeUsingInorderAndPostorder { publi ...

  4. 【题解二连发】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 - LeetCode Construct Binary ...

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

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

  6. Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: ...

  7. leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp; Construct Binary Tree f

    1.  Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...

  8. (二叉树 递归) leetcode 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 that ...

  9. [LeetCode] 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 that ...

随机推荐

  1. 利用asset存储mesh

    做mesh导出的时候遇到了这个问题. 最后解决: 存储mesh数据:AssetDatabase.CreateAsset(meshfilter.mesh, "Assets/" + & ...

  2. Coursera machine learning 第二周 编程作业 Linear Regression

    必做: [*] warmUpExercise.m - Simple example function in Octave/MATLAB[*] plotData.m - Function to disp ...

  3. 九度OJ 1340:小A的计算器 (进制转换)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:735 解决:202 题目描述: 以往的操作系统内部的数据表示都是二进制方式,小A新写了一个操作系统,系统内部的数据表示为26进制,其中0-2 ...

  4. phpstorm+xdebug, 实现断点调试: xdebug如何配置

    [XDebug] xdebug.profiler_output_dir="D:\phpStudy\tmp\xdebug" xdebug.trace_output_dir=" ...

  5. PAT 1059. C语言竞赛(20)

    C语言竞赛是浙江大学计算机学院主持的一个欢乐的竞赛.既然竞赛主旨是为了好玩,颁奖规则也就制定得很滑稽: 0. 冠军将赢得一份“神秘大奖”(比如很巨大的一本学生研究论文集……). 1. 排名为素数的学生 ...

  6. RedisTemplate操作Redis

    RedisTemplate Redis 可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字符串).List(列表).Set(集合).Hash(散列)和 Zset(有序 ...

  7. STL+位运算的文件

    1.queue 队列 queue的头文件是<queue>. 定义queue对象的示例代码如: queue<int>q;  队列内存放的是int类型的数 queue<dou ...

  8. mac工作软件推荐-iterm + zsh + tmux

    原文链接: http://ju.outofmemory.cn/entry/57244 tmux安装https://blog.csdn.net/nmgzywd/article/details/50915 ...

  9. UVA 1664 Conquer a New Region (并查集+贪心)

    并查集的一道比较考想法的题 题意:给你n个点,接着给你n-1条边形成一颗生成树,每条边都有一个权值.求的是以一个点作为特殊点,并求出从此点出发到其他每个点的条件边权的总和最大,条件边权就是:起点到终点 ...

  10. jquery插件之jquery.extend和jquery.fn.extend的区别

    jquery.extend jquery.extend(),是拓展jquery这个类,即可以看作是jquery这个类本身的静态方法,例如: <!DOCTYPE html> <html ...