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. Spring AOP之Introduction(@DeclareParents)简介

    Spring的文档上对Introduction这个概念和相关的注解@DeclareParents作了如下介绍: Introductions (known as inter-type declarati ...

  2. Java用JSONObject-lib来解析json串

    直接贴代码:(所需jar包:json-lib.jar,可能会关联一些其它的jar包,请自行搜索.) import java.util.ArrayList; import java.util.HashM ...

  3. 横竖屏切换时不销毁当前activity 和 锁定屏幕

    首先在Mainifest.xml的Activity元素中加入android:configChanges="orientation|keyboardHidden"属性 <act ...

  4. android dialog加载中动画效果

    //显示动画 dialog = new Dialog(context, R.style.loading); dialog.setContentView(R.layout.loadinglayout); ...

  5. Android----输入模式设置

    InputType的参数: 用法:((EditText)findViewById(R.id.edit)).setInputType(InputType.*); int TYPE_CLASS_DATET ...

  6. 【svn】解析subversion的使用

    目录结构: contents structure [-] 安装客户端 安装服务端 创建仓库 启动仓库 创建客户端与仓库取得联系 使用svn服务 SVN密码管理 SVN的仓库布局和常规命令 分支.合并和 ...

  7. Java后端,应该日常翻看的中文技术网站

    参考链接:http://calvin1978.blogcn.com/articles/dailysites.html

  8. mysql快速移植表数据

    使用select into outfile xxx ,  load data infile xxx 例如 : SELECT * into outfile '/tmp/out.txt' FROM `db ...

  9. Android App解决卡顿慢之内存抖动及内存泄漏(发现和定位)

    内存抖动是指在短时间内有大量的对象被创建或者被回收的现象,内存抖动出现原因主要是频繁(很重要)在循环里创建对象(导致大量对象在短时间内被创建,由于新对象是要占用内存空间的而且是频繁,如果一次或者两次在 ...

  10. FreeSWITCH取消Digest校验流程

    方法一.通过使用acl配置 <list name="domains" default="deny"> <!-- domain= is spec ...