【问题描述】[中等]

【解答思路】



public class Solution {public TreeNode buildTree(int[] inorder, int[] postorder) {int inLen = inorder.length;int postLen = postorder.length;// 特判if (inLen != postLen) {throw new RuntimeException("输入错误");}return buildTree(inorder, 0, inLen - 1, postorder, 0, postLen - 1);}/*** 使用中序遍历序列 inorder 的子区间 [inLeft, inRight]* 与后序遍历序列 postorder 的子区间 [postLeft, postRight] 构建二叉树** @param inorder   中序遍历序列* @param inLeft    中序遍历序列的左边界* @param inRight   中序遍历序列的右边界* @param postorder 后序遍历序列* @param postLeft  后序遍历序列的左边界* @param postRight 后序遍历序列的右边界* @return 二叉树的根结点*/private TreeNode buildTree(int[] inorder, int inLeft, int inRight,int[] postorder, int postLeft, int postRight) {if (inLeft > inRight || postLeft > postRight) {return null;}int pivot = postorder[postRight];int pivotIndex = inLeft;// 注意这里如果编写不当,有数组下标越界的风险while (inorder[pivotIndex] != pivot) {pivotIndex++;}TreeNode root = new TreeNode(pivot);root.left = buildTree(inorder, inLeft, pivotIndex - 1,postorder, postLeft, postRight - inRight + pivotIndex - 1);root.right = buildTree(inorder, pivotIndex + 1, inRight,postorder, postRight - inRight + pivotIndex, postRight - 1);return root;}
}作者:liweiwei1419
链接:https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/solution/hou-xu-bian-li-python-dai-ma-java-dai-ma-by-liwe-2/

HashMap优化



class Solution {HashMap<Integer,Integer> memo = new HashMap<>();int[] post;public TreeNode buildTree(int[] inorder, int[] postorder) {for(int i = 0;i < inorder.length; i++) memo.put(inorder[i], i);post = postorder;TreeNode root = buildTree(0, inorder.length - 1, 0, post.length - 1);return root;}public TreeNode buildTree(int is, int ie, int ps, int pe) {if(ie < is || pe < ps) return null;int root = post[pe];int ri = memo.get(root);TreeNode node = new TreeNode(root);node.left = buildTree(is, ri - 1, ps, ps + ri - is - 1);node.right = buildTree(ri + 1, ie, ps + ri - is, pe - 1);return node;}
}作者:reals
链接:https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/solution/tu-jie-gou-zao-er-cha-shu-wei-wan-dai-xu-by-user72/

copyOfRange 左必右开

class Solution {public TreeNode buildTree(int[] inorder, int[] postorder) {if(inorder==null || postorder==null) {return null;}return helper(inorder,postorder);}private TreeNode helper(int[] in, int[] post) {if(in.length==0) {return null;}//根据后序数组的最后一个元素,创建根节点TreeNode root = new TreeNode(post[post.length-1]);//在中序数组中查找值等于【后序数组最后一个元素】的下标for(int i=0;i<in.length;++i) {if(in[i]==post[post.length-1]) {//确定这个下标i后,将中序数组分成两部分,后序数组分成两部分int[] inLeft = Arrays.copyOfRange(in,0,i);int[] inRight = Arrays.copyOfRange(in,i+1,in.length);int[] postLeft = Arrays.copyOfRange(post,0,i);int[] postRight = Arrays.copyOfRange(post,i,post.length-1);//递归处理中序数组左边,后序数组左边root.left = helper(inLeft,postLeft);//递归处理中序数组右边,后序数组右边root.right = helper(inRight,postRight);break;}}return root;}
}作者:wang_ni_ma
链接:https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/solution/liang-chong-shi-xian-dong-hua-yan-shi-106-cong-zho/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

【总结】

1.前中后序遍历变化的是[中]的位置,左到右的顺序不改变
  • 前序遍历 中左右
  • 中序遍历 左中右
  • 后续遍历 左右中
2.还原二叉树 借助HashMap or copyOfRange

根据前序和后序遍历构造二叉树
[Leetcode][第889题][JAVA][根据前序和后序遍历构造二叉树][分治][递归]
前序+中序遍历可画出原二叉树
[Leedcode][JAVA][第105题][从前序与中序遍历序列构造二叉树][栈][递归][二叉树]
后续+中序遍历可画出原二叉树
[Leetcode][第106题][JAVA][ 从中序与后序遍历序列构造二叉树][分治][递归]

3. 多画图 写写写 遍历代码 手撕变量 大脑保持清醒

转载链接:https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/solution/hou-xu-bian-li-python-dai-ma-java-dai-ma-by-liwe-2/
转载链接:https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/solution/tu-jie-gou-zao-er-cha-shu-wei-wan-dai-xu-by-user72/

[Leetcode][第106题][JAVA][ 从中序与后序遍历序列构造二叉树][分治][递归]相关推荐

  1. [Leetcode][第889题][JAVA][根据前序和后序遍历构造二叉树][分治][递归]

    [问题描述][中等] [解答思路] copyOfRange class Solution {public TreeNode constructFromPrePost(int[] pre, int[] ...

  2. [Leedcode][JAVA][第105题][从前序与中序遍历序列构造二叉树][栈][递归][二叉树]

    [问题描述][中等] 根据一棵树的前序遍历与中序遍历构造二叉树.注意: 你可以假设树中没有重复的元素.例如,给出前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = ...

  3. leetcode105. 从前序与中序遍历序列构造二叉树(递归)

    根据一棵树的前序遍历与中序遍历构造二叉树.注意: 你可以假设树中没有重复的元素.例如,给出前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = [9,3,15,20 ...

  4. leetcode 106. 从中序与后序遍历序列构造二叉树 c语言递归解法

    如题: 根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素.例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 postorder = [9, ...

  5. java用中根后根序列构造二叉树,106. 从中序与后序遍历序列构造二叉树

    题目描述 根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 示例: 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 postorder ...

  6. 【LeetCode系列】从中序与后序遍历序列构造二叉树 从前序与中序遍历序列构造二叉树...

    关注上方"深度学习技术前沿",选择"星标公众号", 资源干货,第一时间送达! 105. 从前序与中序遍历序列构造二叉树 根据一棵树的前序遍历与中序遍历构造二叉树 ...

  7. Suzy找到实习了吗Day 18 | 二叉树进行中:513 找树左下角的值,112 路径总和 ,106.从中序与后序遍历序列构造二叉树

    513 找树左下角的值 solution # Definition for a binary tree node. # class TreeNode: # def __init__(self, val ...

  8. 【必拿下系列】106. 从中序与后序遍历序列构造二叉树105从前序与中序遍历序列构造二叉树

    两题各自的链接放这里了: 链接: 106 链接: 105 106.从中序与后序遍历序列构造二叉树 如果你是不知道理论的,那就得仔细分析了, 举个例子: 输入:inorder = [9,3,15,20, ...

  9. 【算法】有序链表转换二叉搜索树和从中序与后序遍历序列构造二叉树Java解答参考

    三道算法题 1.有序链表转换二叉搜索树 Java代码参考 2.从中序与后序遍历序列构造二叉树 Java代码参考 3.移除元素 Java代码参考 1.有序链表转换二叉搜索树 给定一个单链表,其中的元素按 ...

最新文章

  1. r语言ggplot2一夜多图_ggplot2简介
  2. 第三部分:Android 应用程序接口指南---第一节:应用程序组件---第七章 App Widgets...
  3. 去掉linux开关机图片,在Deepin Linux系统下去掉NVIDIA开机Logo的方法
  4. python列表的内置方法_Python内置方法详解
  5. 基于dnn的车牌识别_自然场景中文文字识别,身份证火车票都能识别
  6. Linux 命令之 ulimit 命令-控制shell程序的资源
  7. matlab矩阵 0,matlab zeros初始化为0矩阵
  8. 次时代各制作插件使用方案以及技巧 包括UV 烘焙 减面等
  9. c语言可以调用汇编语言吗,C语言与汇编语言混编方式
  10. interface接口_Java程序设计--接口interface(笔记)
  11. 《『若水新闻』客户端开发教程》——10.代码编写(2)
  12. 程序员必修课--sql思维举重训练
  13. [Java] 蓝桥杯ALGO-63 算法训练 乘法表
  14. 多线程往文件里写数据
  15. mysql 导入,导出数据库
  16. matplotlib科研绘图---Times New Roman字体设置
  17. openstack-nova-api源码剖析1
  18. 根据时间进行视频的裁剪
  19. 安装gensim库的方法最终解答!
  20. Windows删除文件的打开方式

热门文章

  1. 洛谷——P1067 多项式输出
  2. [原创] 毕设---在myeclipes中安装Hadoop开发插件
  3. Catalan数(数论)
  4. 《linux内核设计与实现》读书笔记第一、二章
  5. Ruby学习-第二章
  6. 和菜鸟一起深入学习国嵌实验之简单Makefile
  7. 在虚拟机中安装和配配置 MOSS2007 全过程
  8. Error: Cannot find module 'json-schema-faker' YAPI部署
  9. 微信小程序获取当前时间戳、获取当前时间、时间戳加减
  10. mysql varchar 长度限制_MySQL数据库varchar的限制规则说明