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 duplicates do not exist in the tree.

Hide Tags

Tree Array Depth-first Search

 

SOLUTION 1:

使用递归的思想,先找到根节点(它就是post order最后一个),然后再在inorder中找到它,以确定左子树的node个数。
然后分别确定左子树右子树的左右边界
例子:
{4, 5, 2, 7, 8, 1, 3}这树的
inorder: 7 5 8 | 4 | 1 2 3
post: 7 8 5 | 1 3 2 | 4
以上我们可以看到左右子树的划分关系。
 /**
* 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 == null || postorder == null) {
return null;
} return dfs(inorder, postorder, 0, inorder.length - 1, 0, postorder.length - 1);
} public TreeNode dfs(int[] inorder, int[] postorder, int inL, int inR, int postL, int postR) {
if (inL > inR) {
return null;
} // create the root node.
TreeNode root = new TreeNode(postorder[postR]); // find the position of the root node in the inorder traversal.
int pos = 0;
for (; pos <= inR; pos++) {
if (inorder[pos] == postorder[postR]) {
break;
}
} int leftNum = pos - inL; root.left = dfs(inorder, postorder, inL, pos - 1, postL, postL + leftNum - 1);
root.right = dfs(inorder, postorder, pos + 1, inR, postL + leftNum, postR - 1); return root;
}
}

代码: https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/BuildTree2.java

LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告的更多相关文章

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

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

  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 由中序和后序遍历建立二叉树

    Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...

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

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

  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】889. Construct Binary Tree from Preorder and Postorder Traversal 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  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

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

随机推荐

  1. 你想要的iOS 小技巧总结

    UITableView的Group样式下顶部空白处理 //分组列表头部空白处理 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(, , ...

  2. iphone3g 蜂窝数据有效设置

    iphone3g 蜂窝数据有效设置 蜂窝数据     APN cmnet/空     用户名 空     A密码 空彩信(默认为空,需要控制的话,可以设置)     APN cmwap/空     用 ...

  3. mac系统下安装mysql 和phpmyadmin

    用惯了在Windows上一个appserv或phpStudy压缩包搞定,要在MAC OSX 10.10上捣腾一个PHP开发环境还不大习惯.但自己亲自配一下环境还是有所收获的.众所周知OSX上自带了ap ...

  4. PowerDesigner使用:[3]创建索引

    PowerDesigner是一款功能非常强大的建模工具软件,足以与Rose比肩,同样是当今最著名的建模软件之一.Rose是专攻UML对象模型的建模工具,之后才向数据库建模发展,而PowerDesign ...

  5. 解决UEditor将div标签换成p标签的问题

    原文链接 将设计排版好的页面html代码上传到数据库,再读取出来的时候发现所有的div都被替换成了p标签. 解决方法: 首先在ueditor.all.js文件内搜索allowDivTransToP,找 ...

  6. apktool 在mac下的使用 -反编译安卓apk文件

    1.下载apktool 点击这里下载 ,里面有两个文件,一个是.jar,一个是自己写的脚本.sh 注:最新的apktool.jar 文件可以点击这里下载 .sh脚本是自写脚本可不用更新最新,下载的ja ...

  7. SQL Performance Analyzer

    SQL Performance Analyzer 系统发生变更,比如升级数据库.增加索引,都会可能导致sql的执行计划发生改变,从而影响sql的性能. 如果能预知系统变更会对sql的性能的影响,就可以 ...

  8. 谁说C语言很简单?

    前两天,Neo写了一篇<语言的歧义>其使用C语言讨论了一些语言的歧义.大家应该也顺便了解了一下C语言中的很多不可思异的东西,可能也是你从未注意到的东西. 是的,C语言并不简单,让我们来看看 ...

  9. nginx last 和break redirect 和 permanent

    一.last & break (1)last 和 break 当出现在location 之外时,两者的作用是一致的没有任何差异. 注意一点就是,他们会跳过所有的在他们之后的rewrite 模块 ...

  10. Android帧布局<TabHost>标签

    先贴上一段代码: main.xml: <p><?xml version="1.0" encoding="utf-8"?> <Tab ...