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

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


题目标签:Array, Tree

  这到题目和105 几乎是一摸一样的,唯一的区别就是把pre-order 换成 post-order。因为post-order是最后一个数字是root,所以要从右向左的遍历。还需要把helper function 里的 right child 和 left child 顺序更换一下,并且要把相关的代入值也改一下。具体可以看code。

Java Solution:

Runtime beats 68.55%

完成日期:08/26/2017

关键词:Array, Tree

关键点:递归;利用post-order 和 in-order 的位置关系递归

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution
{
public TreeNode buildTree(int[] inorder, int[] postorder)
{
Map<Integer, Integer> inMap = new HashMap<Integer, Integer>(); // save inorder number as key, position as value into map
for(int i=0; i<inorder.length; i++)
inMap.put(inorder[i], i); TreeNode root = helper(postorder, postorder.length-1, 0, inorder.length - 1, inMap); return root;
} public TreeNode helper(int[] postorder, int postEnd, int inStart, int inEnd,
Map<Integer, Integer> inMap)
{
if(inStart > inEnd)
return null; int rootVal = postorder[postEnd];
TreeNode root = new TreeNode(rootVal);
int inRoot = inMap.get(rootVal); // position in inOrder /* inStart & inEnd: for left child, move inEnd to the left of root
* for right child, move inStart to the right of root */
root.right = helper(postorder, postEnd - 1, inRoot + 1, inEnd, inMap);
/* postStart: for right left, go to inorder to check how many right children does root have,
* add it into postorder to skip them to reach left child */
root.left = helper(postorder, postEnd - (inEnd - inRoot) - 1, inStart, inRoot - 1, inMap); return root;
}
}

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal (用中序和后序树遍历来建立二叉树)的更多相关文章

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

  2. LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++

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

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

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

  5. C#解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 ...

  6. [leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal(medium)

    原题地址 思路: 和leetcode105题差不多,这道题是给中序和后序,求出二叉树. 解法一: 思路和105题差不多,只是pos是从后往前遍历,生成树顺序也是先右后左. class Solution ...

  7. Leetcode#106 Construct Binary Tree from Inorder and Postorder Traversal

    原题地址 二叉树基本操作 [       ]O[              ] [       ][              ]O 代码: TreeNode *restore(vector<i ...

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

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

  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. AIX盘rw_timeout值过小导致IO ERROR

    刚下班没多久,接收到告警提示数据库的数据文件异常,且同时收到主机硬盘的IO ERROR告警 该数据库服务器为AIX+oracle 9i环境,登录主机验证关键日志告警 发现确实在18点48分有磁盘IO的 ...

  2. linux 下怎样查找一个文件夹在哪个目录下?

    如果只显示所在目录的路径: find 目录 -type d -name "查询目录名" -printf "%h\n" 如果同时显示目录名称和所在目录的路径: f ...

  3. CustomDialog——一个多功能、通用、可自定义的Dialog

    CustomDialog--一个多功能.通用.可自定义的Dialog  依赖 compile 'com.github.SiberiaDante:CustomDialog:v1.0.1'  说明[Cus ...

  4. Hibernate注解配置与XML配置区别

    注解配置的方式与xml很很多类似: 首先是需要加入4个jar包:hibernate-commons-annotations.jar . hibernate-annotations.jar.ejb3-p ...

  5. Java图的邻接矩阵实现

    /** * * 图的邻接矩阵实现 * @author John * * @param <T> */ class AMWGraph<T> { private ArrayList& ...

  6. flex布局常用属性

    最近喜欢flex布局,它可以完美的实现响应式布局,下边我总结了它的一些常用属性,喜欢的,也可以练习写一下,很好用~~~ 注意:使用了flex布局,对于子元素的float.clear和vertical- ...

  7. mongoDB rs和sh关键字的作用

    rs是一个全局变量,其其中定义了很多分片操作的辅助函数, 可以sh.help()查看使用的辅助函数 这些是rs和sh所有函数 在mongoDB中有两种方法表示 例 sh.enableSharding( ...

  8. cocos2dx 中触摸事件分发一些解读

    触摸事件分发中几个代码解读: 怎么说呢,感觉cocos2dx中的消息分发机制,相对于android中触摸事件分发机制要简单的多.因为android中要做区域判断,过滤器,以及父子组件分发给谁等等的逻辑 ...

  9. angular学习笔记01

    angular.js路由功能 用于实现单页应用 //html 代码 <div ng-view></div> //js代码 angular.module('myM1',['ng' ...

  10. 尝试在Linux上部署Asp.net Core应用程序

    快两个月没接触.net,倒是天天在用Linux,所以想尝试一下在Linux运行喜欢的.net 应用. 安装CentOS 安装.Net core for Linux 创建Asp.net Core应用程序 ...