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

  1. inorder = [9,3,15,20,7]
  2. postorder = [9,15,7,20,3]

Return the following binary tree:

  1. 3
  2. / \
  3. 9 20
  4. / \
  5. 15 7
  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. public TreeNode buildTree(int[] inorder, int[] postorder) {
  12. Map<Integer, Integer> mymap = new HashMap<>();
  13. for (int i = 0; i < inorder.length; i++) {
  14. mymap.put(inorder[i], i);
  15. }
  16. return helper(0, inorder.length - 1, 0, postorder.length - 1, postorder, mymap);
  17. }
  18.  
  19. private TreeNode helper(int inLeft, int inRight, int postLeft, int postRight, int[] postorder, Map<Integer, Integer> mymap) {
  20. if (inLeft > inRight) {
  21. return null;
  22. }
  23. TreeNode cur = new TreeNode(postorder[postRight]);
  24. int index = mymap.get(postorder[postRight]);
  25. int leftSize = index - inLeft;
  26. // postRight for left just add up leftSize
  27. cur.left = helper(inLeft, index - 1, postLeft, postLeft + leftSize - 1, postorder, mymap);
  28. cur.right = helper(index + 1, inRight, postLeft + leftSize, postRight - 1, postorder, mymap);
  29. return cur;
  30. }
  31. }

[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. 学生信息的添加 Java web简单项目初试(失败)

    题目要求: 1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母.数字组成.(1分) 3性别:要求用单 ...

  2. .NETCore部署步骤

    1.下载.NET CORE运行时 下载地址:https://dotnet.microsoft.com/download 2.windows安装下载的运行时 3.检查.是否安装成功 ,dotnet -- ...

  3. Go mod graphql-go 的 Replace

    现在在项目中大量的使用 graphql,但用的版本是3年前的版本. 3年前包的url:github.com/neelance/graphql-go 现在的url:github.com/graph-go ...

  4. 为什么常用 Map<> map = new HashMap()

    在初学Java的时候,经常能看到教材上的写法,使用了接口Map来引用一个map,而不是它的具体实现,那么这样做的好处是什么呢? <Effective Java>第52条:通过接口引用对象 ...

  5. 给锚点a标签添加滑动效果

    a标签是前端必用之一,但是a标签点击后马上跳到了href属性值处,有时候要达到滑动效果就要自己添加JavaScript 普通的a标签代码写好之后,在js脚本内加上 $("a").c ...

  6. 01 语言基础+高级:1-10 JDK8新特性_day12【函数式接口】

    day12[函数式接口] 主要内容自定义函数式接口函数式编程常用函数式接口 教学目标能够使用@FunctionalInterface注解能够自定义无参无返回函数式接口能够自定义有参有返回函数式接口能够 ...

  7. AFN Post请求,报错400(code:-1011)

    解决方法: 声明请求的参数格式是json, post的数据格式还是传字典. 声明代码: AFHTTPSessionManager *manager = [AFHTTPSessionManager ma ...

  8. MySql 相关面试题

    1.mysql 慢查询 目的:通过慢查询日志,记录超过指定时间的 SQL 语句,优化 sql 查询 步骤:查看慢查询开启状态-->设置慢查询 http://www.cnblogs.com/luy ...

  9. Linux文件共享的实现方式

    前两天跟老师去北京开了一个会议,好久没学习了,今天才回学校,其中的辛酸就不说了.来正文: 1.什么是文件共享 (1).文件共享就是同一个文件(同一个文件指的是同一个inode,同一个pathname) ...

  10. 关于本人:-D(必读)

    关于本人 本人目前从事iOS开发,但心中也有一个全栈的梦想,希望与大家共勉! 关于博客内容 自己会不时分享一些iOS方面的技术点.总结的一些经验及工具类.还有学习其他语言过程中的笔记.技术.总结及心得 ...