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 的位置关系递归

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution
  11. {
  12. public TreeNode buildTree(int[] inorder, int[] postorder)
  13. {
  14. Map<Integer, Integer> inMap = new HashMap<Integer, Integer>();
  15.  
  16. // save inorder number as key, position as value into map
  17. for(int i=0; i<inorder.length; i++)
  18. inMap.put(inorder[i], i);
  19.  
  20. TreeNode root = helper(postorder, postorder.length-1, 0, inorder.length - 1, inMap);
  21.  
  22. return root;
  23. }
  24.  
  25. public TreeNode helper(int[] postorder, int postEnd, int inStart, int inEnd,
  26. Map<Integer, Integer> inMap)
  27. {
  28. if(inStart > inEnd)
  29. return null;
  30.  
  31. int rootVal = postorder[postEnd];
  32. TreeNode root = new TreeNode(rootVal);
  33. int inRoot = inMap.get(rootVal); // position in inOrder
  34.  
  35. /* inStart & inEnd: for left child, move inEnd to the left of root
  36. * for right child, move inStart to the right of root */
  37. root.right = helper(postorder, postEnd - 1, inRoot + 1, inEnd, inMap);
  38. /* postStart: for right left, go to inorder to check how many right children does root have,
  39. * add it into postorder to skip them to reach left child */
  40. root.left = helper(postorder, postEnd - (inEnd - inRoot) - 1, inStart, inRoot - 1, inMap);
  41.  
  42. return root;
  43. }
  44. }

参考资料: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. Python数据类型方法精心整理,不必死记硬背,看看源码一切都有了

    Python认为一切皆为对象:比如我们初始化一个list时: li = list('abc') 实际上是实例化了内置模块builtins(python2中为__builtin__模块)中的list类: ...

  2. temp-内外网同时上的例子

    @echo off rem //不少公司的网管试图解决双网卡问题,下面我就给大家详细的讲解一下双网卡同时使用的方法,这样即可保障内网的安全,又能解决电脑访问外网的问题,一举两得.希望大家喜欢.rem ...

  3. Struts访问的时候出现request=null的情况

    今天用Struts框架写个小应用的时候,出现了如下问题 private File upload;    private String uploadContentType;    private Str ...

  4. Java开发规范总结(两周至少看一次)

     Service / DAO 层方法命名规约: 1 ) 获取单个对象的方法用 get 做前缀.2 ) 获取多个对象的方法用 list 做前缀.3 ) 获取统计值的方法用 count 做前缀.4 ) 插 ...

  5. [UIKit学习]06.懒加载,模型,自定义代码段,重写构造方法

    懒加载 在get中加载,且只加载一次 - (NSArray *)shops { if (_shops == nil) { NSString *file = [[NSBundle mainBundle] ...

  6. PYTHON 函数局部变量和全局变量

    有这样一段PYTHON代码,从事C语言开发的人都知道,如果定义了全局变量,而函数内没有定义同名的函数变量的话,那么在函数内对该变量的赋值就是对全局变量空间数值的修改, 然后在PYTHON中却不尽相同, ...

  7. [js高手之路] html5 canvas系列教程 - arc绘制曲线图形(曲线,弧线,圆形)

    绘制曲线,经常会用到路径的知识,如果你对路径有疑问,可以参考我的这篇文章[js高手之路] html5 canvas系列教程 - 开始路径beginPath与关闭路径closePath详解. arc:画 ...

  8. Akka(27): Stream:Use case-Connecting Slick-dbStream & Scalaz-stream-fs2

    在以前的博文中我们介绍了Slick,它是一种FRM(Functional Relation Mapper).有别于ORM,FRM的特点是函数式的语法可以支持灵活的对象组合(Query Composit ...

  9. Linux学习——shell编程之环境变量配置文件

    小白学习,在学习中总结! shell编程之环境变量配置文件 一:环境变量配置文件 1 shell编程之环境变量配置 变量类型: 用户自定义变量(本地变量) 环境变量 :定义每个用户的操作环境,如pat ...

  10. 再起航,我的学习笔记之JavaScript设计模式27(链模式)

    链模式 概念介绍 链模式(Operatc of Responsibility): 通过在对象方法中将当前对象返回,实现对同一个对象多个方法的链式调用.从而简化对该对象的多个方法的多次调用时,对该对象的 ...