先序、中序和后序数组两两结合重构二叉树

package chapter_3_binarytreeproblem;import java.util.HashMap;public class Problem_21_PreInPosArrayToTree {public static class Node {public int value;public Node left;public Node right;public Node(int data) {this.value = data;}}public static Node preInToTree(int[] pre, int[] in) {if (pre == null || in == null) {return null;}HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();for (int i = 0; i < in.length; i++) {map.put(in[i], i);}return preIn(pre, 0, pre.length - 1, in, 0, in.length - 1, map);}public static Node preIn(int[] p, int pi, int pj, int[] n, int ni, int nj,HashMap<Integer, Integer> map) {if (pi > pj) {return null;}Node head = new Node(p[pi]);int index = map.get(p[pi]);head.left = preIn(p, pi + 1, pi + index - ni, n, ni, index - 1, map);head.right = preIn(p, pi + index - ni + 1, pj, n, index + 1, nj, map);return head;}public static Node inPosToTree(int[] in, int[] pos) {if (in == null || pos == null) {return null;}HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();for (int i = 0; i < in.length; i++) {map.put(in[i], i);}return inPos(in, 0, in.length - 1, pos, 0, pos.length - 1, map);}public static Node inPos(int[] n, int ni, int nj, int[] s, int si, int sj,HashMap<Integer, Integer> map) {if (si > sj) {return null;}Node head = new Node(s[sj]);int index = map.get(s[sj]);head.left = inPos(n, ni, index - 1, s, si, si + index - ni - 1, map);head.right = inPos(n, index + 1, nj, s, si + index - ni, sj - 1, map);return head;}// 每个节点的孩子数都为0或2的二叉树才能被先序与后序重构出来public static Node prePosToTree(int[] pre, int[] pos) {if (pre == null || pos == null) {return null;}HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();for (int i = 0; i < pos.length; i++) {map.put(pos[i], i);}return prePos(pre, 0, pre.length - 1, pos, 0, pos.length - 1, map);}public static Node prePos(int[] p, int pi, int pj, int[] s, int si, int sj,HashMap<Integer, Integer> map) {Node head = new Node(s[sj--]);if (pi == pj) {return head;}int index = map.get(p[++pi]);head.left = prePos(p, pi, pi + index - si, s, si, index, map);head.right = prePos(p, pi + index - si + 1, pj, s, index + 1, sj, map);return head;}// for test -- print treepublic static void printTree(Node head) {System.out.println("Binary Tree:");printInOrder(head, 0, "H", 17);System.out.println();}public static void printInOrder(Node head, int height, String to, int len) {if (head == null) {return;}printInOrder(head.right, height + 1, "v", len);String val = to + head.value + to;int lenM = val.length();int lenL = (len - lenM) / 2;int lenR = len - lenM - lenL;val = getSpace(lenL) + val + getSpace(lenR);System.out.println(getSpace(height * len) + val);printInOrder(head.left, height + 1, "^", len);}public static String getSpace(int num) {String space = " ";StringBuffer buf = new StringBuffer("");for (int i = 0; i < num; i++) {buf.append(space);}return buf.toString();}public static void main(String[] args) {int[] pre = { 1, 2, 4, 5, 8, 9, 3, 6, 7 };int[] in = { 4, 2, 8, 5, 9, 1, 6, 3, 7 };int[] pos = { 4, 8, 9, 5, 2, 6, 7, 3, 1 };printTree(preInToTree(pre, in));printTree(inPosToTree(in, pos));printTree(prePosToTree(pre, pos));}}

参考资料:

【1】程序员代码面试指南  -- 左程云

先序、中序和后序数组两两结合重构二叉树 -- 图解相关推荐

  1. java根据前序和中序建树_(Java实现)二叉树---根据前序、中序、后序数组还原二叉树...

    概述在上一篇文章中讲到顺序存储二叉树,一般是用于完全二叉树,通过统一的数学公式可以将数组还原成完全二叉树 而对于普通的二叉树来说,也可以根据前序.中序和后序遍历得到的数组,还原二叉树 还原还原的情况分 ...

  2. 二叉树前序后序中序互推总结

    最近笔试题一直遇到这个题,今天就总结一下.本文主要回答以下几个问题(Java实现): 前序 + 中序 => 后序 + 层序 后序 + 中序 => 前序 + 层序 以上2个问题的2种解决办法 ...

  3. 二叉树:给后序中序遍历,求前序

    给出后序中序, 求前序: 1.给后序中序求前序,因为后序序列左右根从后往前是根右左的顺序.因此递归先造右子树,再造左子树. 2.给前序中序求后序,因为前序序列是根左右,从前往后是根左右.因此递归先造左 ...

  4. 二叉树--先序中序遍历求后序遍历

    先序遍历:根 左 右 中序遍历:左 根 右 后序遍历:左 右 根 我们可以先从先序遍历中找到根节点,由于知道了根节点那么可以依靠中序遍历找到左子树,右子树.这样再去先序遍历中找到左子树的根节点,然后再 ...

  5. 二叉树前序中序后续线索树_二叉树的先序,中序,后序遍历以及线索二叉树的遍历...

    二叉树的先序,中序,后序遍历以及线索二叉树的遍历 (2008-05-04 17:52:49) 标签: 杂谈 C++ 二叉树的先序,中序,后序遍历以及线索二叉树的遍历 头文件 //*********** ...

  6. 【POJ - 2255】Tree Recovery (给定树的先序中序,输出后序)

    题干: Input The input will contain one or more test cases.  Each test case consists of one line contai ...

  7. C/C++面试题—重建二叉树【前序 + 中序- 重建二叉树 和 后序 + 中序 - 重建二叉树】

    题目介绍 题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字. 例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{ ...

  8. 二叉树前序中序,后序中序,公共最近祖先的实现

    二叉树前序中序,后序中序,公共最近祖先的实现 注释中详细介绍了算法,故不再赘述. 无论是前序还是后序,一个节点的左子树和右子树都是可以看做是分开的,有一定规律可循,故可用递归进行实现. #includ ...

  9. 二叉树的构造(前序+中序)---(后序 + 中序)

    二叉树的构造(前序+中序)-(后序 + 中序) 思路:要对前序+中序(后序+中序)的构建树的动态过程要了解,思路比较简单,在了解了这个过程之后,理解下面代码就容易了. 过程 参考图: 前序 + 中序: ...

  10. 【算法】【树】已知先序中序序列求后序序列(详细解释)

    题目描述 如题所示,已知先序中序序列建树与求后序序列 算法原理 利用递归和分制的思想,找到当前树先序序列的根节点,然后找到对应中序序列的位置,然后根据根节点在中序序列中的位置来判断左右子树分别的位置, ...

最新文章

  1. 【Codeforces】920A Water The Garden(浇花)
  2. MySQL数学函数简明总结
  3. docker之手动构建新的镜像
  4. PAT(乙级) 1002 写出这个数 (20point(s)) Python
  5. mac php errorlog,Mac下使用php的error_log()函数发送邮件
  6. mysql如何逻辑删除_mysql逻辑删除如何恢复
  7. java nio集群_java – Hazelcast:连接到远程集群
  8. Python美味食谱:1.6 合并字符串
  9. Rhino(犀牛)的视口
  10. windows杀进程
  11. Windows XP_修改登录背景图案
  12. MacBook没声音
  13. 填万能经营范围模板,避开办理营业执照经营范围的坑
  14. [SAE]免费服务器:新浪云服务器SAE的注册与使用
  15. 2021年3月最新的山东大学网络认证的网址
  16. Mac 移动硬盘突然自己异常退出了(一)
  17. OpenCV常用函数极简简介
  18. ccs C语言编译器,CCS C 编译器手册2010.pdf
  19. 区块链100讲:Truffle——一个更简单的部署智能合约的方法
  20. C/C++学习日记:用C语言画一个爱心,哄女朋友效果杠杠的!

热门文章

  1. Usb设备驱动5:usb-firmware简易框架
  2. 【转】Content-type的几种常见类型
  3. 暑假集训 || 线段树
  4. python3.7 倒计时
  5. P3986 斐波那契数列
  6. spring 多线程 写入数据库 和 写入 xml文件
  7. Java中将16进制字符串转换成汉字
  8. 方维分享系统修改,本地安装失败,后台无法登陆
  9. 游戏筑基开发之字符串的注意点(C语言)
  10. Linux之动态网页——搭建博客