一、已知前序、中序、后序遍历结果的其中两种,还原二叉树。

①已知前序遍历结果:1,2,4,5,3,6,7
中序遍历结果:4,2,5,1,6,3,7
还原二叉树后BFS出结果。

TreeNode.java

public class TreeNode {
private TreeNode leftChild;
private TreeNode rightChild;
private Object data; public TreeNode getLeftChild() {
return leftChild;
} public void setLeftChild(TreeNode leftChild) {
this.leftChild = leftChild;
} public TreeNode getRightChild() {
return rightChild;
} public void setRightChild(TreeNode rightChild) {
this.rightChild = rightChild;
} public Object getData() {
return data;
} public void setData(Object data) {
this.data = data;
} public TreeNode(Object data) {
super();
this.data = data;
}
}

CreateTree.java:

import java.util.LinkedList;
import java.util.Queue; public class CreateTree {
public static TreeNode genenateTree(int[] pre, int[] in) {
if (pre.length == 0 || in.length == 0) {
return null;
}
TreeNode root = new TreeNode(pre[0]);
int i = 0;
while (in[i] != pre[0]) {
i++;
}
int[] preLeftChild = new int[i];
int[] preRightChild = new int[pre.length - 1 - i];
int[] inLeftChild = new int[i];
int[] inRightChild = new int[pre.length - 1 - i];
for (int j = 0; j < in.length; j++) {
if (j < i) {
preLeftChild[j] = pre[j + 1];
inLeftChild[j] = in[j];
} else if (j > i) {
preRightChild[j - i - 1] = pre[j];
inRightChild[j - i - 1] = in[j];
}
}
root.setLeftChild(genenateTree(preLeftChild, inLeftChild));
root.setRightChild(genenateTree(preRightChild, inRightChild));
return root;
} public static void visited(TreeNode node) {
System.out.print(node.getData() + " ");
} public static void LevenOrder(TreeNode root) {
if (root == null) {
return;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
TreeNode temp = null;
while (!queue.isEmpty()) {
temp = queue.poll();
visited(temp);
if (temp.getLeftChild() != null) {
queue.add(temp.getLeftChild());
}
if (temp.getRightChild() != null) {
queue.add(temp.getRightChild());
}
}
} public static void main(String[] args) {
int[] pre = { 1, 2, 4, 5, 3, 6, 7 };
int[] in = { 4, 2, 5, 1, 6, 3, 7 };
LevenOrder(genenateTree(pre, in));
}
}

  

②已知前序遍历结果:1,2,4,5,3,6,7
后序遍历结果:4,5,2,6,7,3,1
这种情况不能确定唯一的二叉树(即根据前序、后序结果不能确定唯一二叉树)

③已知 中序遍历结果:4,2,5,1,6,3,7
后序遍历结果:4,5,2,6,7,3,1
还原二叉树后BFS出结果。

//这里只写出核心代码,其他部分可以参考第一种情况
public class CreateTree {
public static TreeNode genenateTree(int[] in, int[] post) {
if (post.length == 0 || in.length == 0) {
return null;
}
TreeNode root = new TreeNode(post[post.length - 1]);
int i = 0;
while (in[i] != post[post.length - 1]) {
i++;
}
int[] postLeftChild = new int[i];
int[] postRightChild = new int[post.length - 1 - i];
int[] inLeftChild = new int[i];
int[] inRightChild = new int[post.length - 1 - i];
for (int j = 0; j < in.length; j++) {
if (j < i) {
postLeftChild[j] = post[j];
inLeftChild[j] = in[j];
} else if (j > i) {
postRightChild[j - i - 1] = post[j - 1];
inRightChild[j - i - 1] = in[j];
}
}
root.setLeftChild(genenateTree(inLeftChild, postLeftChild));
root.setRightChild(genenateTree(inRightChild, postRightChild));
return root;
}

  

二、如果已知的前序、中序、后序的结果中包含占位符#,此时,只需知道其中一种遍历结果就能还原二叉树,且结果是唯一的。

①已知前序遍历结果是 :"1", "2", "4", "#", "#", "5", "#", "#", "3", "6", "#", "#", "7", "#", "#",还原二叉树后BFS出结果。

import java.util.LinkedList;
import java.util.Queue; public class CreateTree {
static int count = 0; public static TreeNode genenateTree(String[] data) {
TreeNode root = null;
if (count >= data.length || data[count++].equals("#")) {
root = null;
} else {
root = new TreeNode(data[count - 1]);
root.setLeftChild(genenateTree(data));
root.setRightChild(genenateTree(data));
}
return root;
} public static void visited(TreeNode node) {
System.out.print(node.getData() + " ");
} public static void LevenOrder(TreeNode root) {
if (root == null) {
return;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
TreeNode temp = null;
while (!queue.isEmpty()) {
temp = queue.poll();
visited(temp);
if (temp.getLeftChild() != null) {
queue.add(temp.getLeftChild());
}
if (temp.getRightChild() != null) {
queue.add(temp.getRightChild());
}
}
} public static void main(String[] args) {
String[] dataStr = { "1", "2", "4", "#", "#", "5", "#", "#", "3", "6", "#", "#", "7", "#", "#" };
LevenOrder(genenateTree(dataStr));
}
}

Java数据结构——根据遍历结果构造二叉树的更多相关文章

  1. Java实现 LeetCode 106 从中序与后序遍历序列构造二叉树

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

  2. Java实现 LeetCode 105 从前序与中序遍历序列构造二叉树

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

  3. lintcode: 中序遍历和后序遍历树构造二叉树

    题目 中序遍历和后序遍历树构造二叉树 根据中序遍历和后序遍历树构造二叉树 样例 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下的树: 2 /  \ 1    3 注意 你可 ...

  4. LeetCode---105. 从前序与中序遍历序列构造二叉树 (Medium)

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

  5. lintcode :前序遍历和中序遍历树构造二叉树

    解题 前序遍历和中序遍历树构造二叉树 根据前序遍历和中序遍历树构造二叉树. 样例 给出中序遍历:[1,2,3]和前序遍历:[2,1,3]. 返回如下的树: 2 / \ 1 3 注意 你可以假设树中不存 ...

  6. 【2】【leetcode-105,106】 从前序与中序遍历序列构造二叉树,从中序与后序遍历序列构造二叉树

    105. 从前序与中序遍历序列构造二叉树 (没思路,典型记住思路好做) 根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [ ...

  7. [leetcode]从中序与后序/前序遍历序列构造二叉树

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

  8. LintCode-72.中序遍历和后序遍历树构造二叉树

    中序遍历和后序遍历树构造二叉树 根据中序遍历和后序遍历树构造二叉树 注意事项 你可以假设树中不存在相同数值的节点 样例 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下的树: ...

  9. LintCode-73.前序遍历和中序遍历树构造二叉树

    前序遍历和中序遍历树构造二叉树 根据前序遍历和中序遍历树构造二叉树. 注意事项 你可以假设树中不存在相同数值的节点 样例 给出中序遍历:[1,2,3]和前序遍历:[2,1,3]. 返回如下的树:    ...

随机推荐

  1. makefile for Linux纯小白版

    某大佬曾说过: 不会makefile,不要说你会linux,因为makefile是合格程序员的必经之路 首先你要有个vi或者emacs 之类的编辑器 编写几个程序 test1.c #include&l ...

  2. 一分钟玩转 Spring IoC

    前言 「上一篇文章」我们对 Spring 有了初步的认识,而 Spring 全家桶中几乎所有组件都是依赖于 IoC 的. 刚开始听到 IoC,会觉得特别高大上,但其实掰开了很简单. 跟着我的脚步,一文 ...

  3. Python获取当前时间_获取格式化时间_格式化日期

    Python获取当前时间_获取格式化时间: Python获取当前时间: 使用 time.time( ) 获取到距离1970年1月1日的秒数(浮点数),然后传递给 localtime 获取当前时间 #使 ...

  4. MySQL在同一个表上,删除查询出来的结果

    背景 有一个程序员员工表(code_user),包含用户id.姓名.掌握的语言. 表数据如下: +---------+-----------+----------+ | user_id | user_ ...

  5. Python File fileno() 方法

    概述 fileno() 方法返回一个整型的文件描述符(file descriptor FD 整型),可用于底层操作系统的 I/O 操作.高佣联盟 www.cgewang.com 语法 fileno() ...

  6. PHP date_sub() 函数

    ------------恢复内容开始------------ 实例 从 2013 年 3 月 15 日减去 40 天: <?php$date=date_create("2013-03- ...

  7. PHP str_shuffle() 函数

    实例 随机地打乱字符串中的所有字符: <?php高佣联盟 www.cgewang.comecho str_shuffle("Hello World");?> 定义和用法 ...

  8. 7.18 NOI模拟赛 树论 线段树 树链剖分 树的直径的中心 SG函数 换根

    LINK:树论 不愧是我认识的出题人 出的题就是牛掰 == 他好像不认识我 考试的时候 只会写42 还有两个subtask写挂了 拿了37 确实两个subtask合起来只有5分的好成绩 父亲能转移到自 ...

  9. 6.6 省选模拟赛 线段 二维数点问题 树套树 CDQ分治

    LINK:线段 还是太菜了 没看出这道题真正的模型 我真是一个典型的没脑子选手. 考虑如何查询答案. 每次在一个线段x的状态被更改后 可以发现有影响的是 和x相连那段极长连续1子段. 设这个子段左端点 ...

  10. docker 常用命令备忘录

    基础命令 docker version docker info docker --help   镜像命令 查看 docker images   其中: REPOSITORY:表示镜像的仓库源 TAG: ...