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

①已知前序遍历结果: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. Laravel 配置 SqlDebug 服务,进行实时监听打印 SQL

    0:释义 什么是服务容器 简而言之,Laravel 服务容器 是一个用于存储绑定组件的盒子,它还会为应用提供所需的服务. Laravel 服务容器是用于管理类的依赖和执行依赖注入的工具,By Lara ...

  2. pdb 进行调试

    import pdb a = 'aaa' pdb.set_trace( ) b = 'bbb' c = 'ccc' final = a+b+c print(final) import pdb a = ...

  3. Python math 模块、cmath 模块

    Python math 模块.cmath 模块 Python 中数学运算常用的函数基本都在 math 模块.cmath 模块中.高佣联盟 www.cgewang.com Python math 模块提 ...

  4. 7.1 NOI模拟赛 计数问题 dp

    还是可以想出来的题目 不过考场上没有想出来 要 引以为戒. 初看觉得有点不可做 10分给到了爆搜. 考虑第一个特殊情况 B排列为1~m. 容易发现A排列中前m个数字 他们之间不能产生交换 且 第k个数 ...

  5. Linux搭建Gitlab(Docker版)

    1.拉取gitlab的docker镜像 #这里使用gitlab的社区版 docker pull gitlab/gitlab-ce 2.启动gitlab容器实例 docker run -d  -p 44 ...

  6. navicat for mysql 连接报错1251的解决方法

    这是因为比较新的mysql版本采用新的保密方式,若要用navicat连接需要改使用到的用户的密码方式:use mysql:ALTER USER 'root'@'localhost' IDENTIFIE ...

  7. 转)Understanding Java Memory Management

    Understanding Java Memory Management - IBM Java Native Interface (JNI) Objects and Code Java Native ...

  8. mysql 常用的数据类型

    数字类:  整数 tinyint     smallint    mediumint    int       bigint 浮点类:float  double 定点类:decimal(M,D) 日期 ...

  9. 认识IPv4分组

    强化一下记忆:以免忘记. 图就不放了. 首部20B (4B的整数倍) 的固定部分12个域,的确很麻烦的:IPv6才8个域,首部长度8B的整数倍 20B分5行吧,每行4B,即32位.第一行,第二行,第三 ...

  10. 分布式任务调度平台 → XXL-JOB 实战

    开心一刻 老师:谁知道鞭炮用英语怎么说? 甲:老师!老师!我知道,鞭炮的英文是pilipala. 老师:那闪电呢? 乙:kucha kucha 老师:那舞狮呢? 丙:dong dong qiang 老 ...