题目

https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/

题解

待优化:可以用 map 存一下每一个前序遍历元素对应的下标,这样更快一些。

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public TreeNode buildTree(int[] inorder, int[] postorder) {return process(inorder, postorder, 0, inorder.length - 1, 0, postorder.length - 1);}public TreeNode process(int[] inorder, int[] postorder, int L1, int R1, int L2, int R2) {if (L1 > R1) return null;TreeNode root = new TreeNode(postorder[R2]);for (int M1 = L1, n = 0; M1 <= R1; M1++, n++) {if (inorder[M1] == postorder[R2]) {root.left = process(inorder, postorder, L1, M1 - 1, L2, L2 + n - 1);root.right = process(inorder, postorder, M1 + 1, R1, L2 + n, R2 - 1);return root;}}return root;}
}

leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal | 106. 从中序后序遍历序列构造二叉树(Java)相关推荐

  1. LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal

    LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal Solution1:我的答案 仿照105题写的答案 / ...

  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. leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal Construct Binary Tree f

    1.  Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...

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

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

  6. LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal

    原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题 ...

  7. leetcode 刷题之路 64 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 ...

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

  9. Construct Binary Tree from Inorder and Postorder Traversal

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

最新文章

  1. oracle schema与mysql_Oracle数据库之Oracle 11g R2 用户与模式(schema)
  2. java jdk动态代理学习记录
  3. Prometheus + Granafa 构建高大上的MySQL监控平台
  4. Python递归算法以及命名空间
  5. JavaScript window.getComputedStyle()
  6. lombok 中的@Data注解
  7. java gz文件解压_java – 批量解压缩.gz文件
  8. 最新70佳单页网站设计案例欣赏(上篇)
  9. Java多线程同步机制
  10. 苹果或推出Windows版Safari浏览器;微软正“取下”Chromium版Edge浏览器的Beta标签;亚马逊申请新专利……...
  11. 可视化:动态气泡图,拿走不谢!
  12. python开发软件的实例-这可能是最好玩的python GUI入门实例!
  13. 2018年python工作好找吗-2018年 Python面试必看的10个问题及答案
  14. 伪静态在webconfig中配置
  15. Windows2000系统下Apache2和PHP4安装终级宝典
  16. 毕设题目:Matlab语音去噪
  17. 《越狱》的中国隐秘流行
  18. Vs2010中文版 使用 .net 3.5 时,智能提示英文变中文
  19. 2020年中华人民共和国行政区划代码
  20. python re模块详解_re模块详解

热门文章

  1. UVA - 12338 Anti-Rhyme Pairs(后缀数组)
  2. 中石油训练赛 - DNA(字符串哈希)
  3. 洛谷 - P2756 飞行员配对方案问题(二分图最大匹配+路径打印)
  4. HDU - Reversi(dfs+水题)
  5. 力扣题458:可怜的小猪
  6. NEFU709(第K个圆的半径)
  7. PTA 寻宝路线 (40 point(s))
  8. QT乱码总结9.编码测试和总结四
  9. 两年前搭建的网狐系统
  10. 4.OD-调试示例1