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

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

For example, given

inorder = [9,3,15,20,7]
postorder = [9,15,7,20,3]

Return the following binary tree:

    3
/ \
9 20
/ \
15 7
/**
* 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> mymap = new HashMap<>();
for (int i = 0; i < inorder.length; i++) {
mymap.put(inorder[i], i);
}
return helper(0, inorder.length - 1, 0, postorder.length - 1, postorder, mymap);
} private TreeNode helper(int inLeft, int inRight, int postLeft, int postRight, int[] postorder, Map<Integer, Integer> mymap) {
if (inLeft > inRight) {
return null;
}
TreeNode cur = new TreeNode(postorder[postRight]);
int index = mymap.get(postorder[postRight]);
int leftSize = index - inLeft;
// postRight for left just add up leftSize
cur.left = helper(inLeft, index - 1, postLeft, postLeft + leftSize - 1, postorder, mymap);
cur.right = helper(index + 1, inRight, postLeft + leftSize, postRight - 1, postorder, mymap);
return cur;
}
}

[LC] 106. 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】106. Construct Binary Tree from Inorder and Postorder Traversal

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

  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. 106. Construct Binary Tree from Inorder and Postorder Traversal根据后中序数组恢复出原来的树

    [抄题]: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assum ...

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

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

  8. 【LeetCode】105 & 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 ...

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

随机推荐

  1. Python-django入门

    上网流程: Django的安装

  2. Spring framework体系架构

    Spring3.x 图中将spring分为5个部分:core.aop.data access.web.test,图中每个圆角矩形都对应一个jar,如果在maven中配置,所有这些jar的"g ...

  3. html5中如何去掉input type date默认样式

    html5中如何去掉input type date默认样式 1.时间选择的种类: HTML代码: 选择日期:<input type="date" value="20 ...

  4. 清除input表单内容

    碰到几次情况,页面刷新或者从上级页面返回表单的内容依然遗留,很影响使用. <form action="" method="" autocomplete=& ...

  5. 装饰器参数加log

    #有多个函数,需要计算他们的执行时间,加logimport timedef logger(flag): def show_time(f): #foo对象 def inner(*x,**y): star ...

  6. deque & list

    deque 双向队列 它也是采用动态数组的方式来管理的提供了随机数组 和vector的区别 1.deque头尾两端可以开放,能够进行快速的插入和删除(vector只能在尾部进行快速的插入和删除) 2. ...

  7. 框架-Spring容器

    1.   Spring Ioc容器 容器是Spring框架的基础,容器会创建.串联.配置对象,并且能管理对象的整个生命周期.如下是代表 Spring工作原理 MetaData 指定哪些对象实例化.配置 ...

  8. zookeeper注册中心和客户端

    1.zookeeper和eureka区别 zookeeper向client进行ping操作,如果不通,就删除client节点 eureka自我保护机制是client向注册中心发送心跳包,如果一定时间内 ...

  9. .net EasyTree显示所级层级(无限级、整层级颗树)的另类写法。非递归

    获取整颗树的另类写法.非递归 //获取所有的菜单 List<T_Menu> menu = bll.getMenuByUsesrID("8189a7c1-6f15-4744-b6c ...

  10. 4)PHP命名规则,传值方式

    (1)命名规则: 包括变量名,类名,接口名函数名等等 ①基本规则: 只能使用小写字母,下划线或者数字 数字不能开头 不能跟环境和系统关键字重复(比如,if,else,function) ② 驼峰式   ...