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. Python面向对象几个知识点

    一.判断对象是否属于类.判断类是否派生自某个类 class Zero: pass class One(Zero): pass class Two(One, Zero): pass # 判断对象是否完全 ...

  2. Install MySQL 5.7 on Fedora 25/24, CentOS/RHEL 7.3/6.8/5.11

    MySQL is a relational database management system (RDBMS) that runs as a server providing multi-user ...

  3. MongoDB Linux环境安装及配置[转]

    CentOS 6.5系统中使用yum安装MongoDB 2.6 教程 CentOS 6.5系统中使用yum安装MongoDB 2.6 教程,本文共分5个步骤完成MongoDB的安装.下面我们在Cent ...

  4. 【struts2】自定义登录检查拦截器

    在实际开发中,一个常见的功能要求是:有很多操作都需要登录后才能操作,如果操作的时候还没有登录,那么通常情况下会要求跳转回到登录页面. 1)如何实现这样的功能呢? 在具体实现之前,先来考虑几个问题: ( ...

  5. opencv 中出现错误 -215:Assertion failed

    cv2.error: OpenCV(4.0.1) D:\Build\OpenCV\opencv-4.0.1\modules\imgproc\src\color.cpp:181: error: (-21 ...

  6. C/C++/动态链接库DLL中函数的调用约定与名称修饰

    参见:http://blog.twofei.com/cc/impl/calling-convension.html 调用约定(Calling Convention)是指在程序设计语言中为了实现函数调用 ...

  7. [转]byte为什么要与上0xFF?

    无意间翻看之间的代码,发现了一段难以理解的代码. byte[] bs = digest.digest(origin.getBytes(Charset.forName(charsetName))) ; ...

  8. echarts 认知笔记

    0.echarts.setOption的核心认知 请注意,它是合并对象,而不是替换对象. 举个简单的例子,如果你第一次setOption时在series中配置了10个对象. 那么你下一次你意图使用只有 ...

  9. 简简单单搞掂恼人的Laravel 5安装

    想折腾下Laravel 5了.Laravel是这世界上最好且没有之一的语言──PHP──的众多框架中的一个,是我比较感兴趣的PHP Web Framework. 但是安装Laravel可不是件容易的事 ...

  10. 绝对路径${pageContext.request.contextPath}用法及其与web.xml中Servlet的url-pattern匹配过程

    以系统的一个“添加商品”的功能为例加以说明,系统页面为add.jsp,如图一所示: 图一  添加商品界面 系统的代码目录结构及add.jsp代码如图二所示: 图二   系统的代码目录结构及add.js ...