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

①已知前序遍历结果: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. PHP getimagesizefromstring - 获取图片信息函数

    getimagesizefromstring — 从字符串中获取图像尺寸信息.高佣联盟 www.cgewang.com 语法 array getimagesizefromstring ( string ...

  2. 玩转 SpringBoot2.x 之整合邮件发送

    序 在实际项目中,经常需要用到邮件通知功能.比如,用户通过邮件注册,通过邮件找回密码等:又比如通过邮件发送系统情况,通过邮件发送报表信息等等,实际应用场景很多. 原文地址:https://www.mm ...

  3. 【洛谷P3802】小魔女帕琪 题解(概率期望)

    前言:蒟蒻太弱了,不会推式子QAQ -------------------- 题目链接 题目大意:给定$7$种能量晶体各$a_i$个,每次随机摸到一个晶体,如果连续摸到$7$个不同的晶体就会触发一次伤 ...

  4. 用 Python 写出这样的进度条,刷新了我对进度条的认知

    ❞ 1 简介 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知识.那么针对这三类人,我给 ...

  5. Python编程的10个经典错误及解决办法

    接触了很多Python爱好者,有初学者,亦有转行人.不论大家学习Python的目的是什么,总之,学习Python前期写出来的代码不报错就是极好的.下面,严小样儿为大家罗列出Python3十大经典错误及 ...

  6. Maven常见异常及解决方法---测试代码编译错误

    [ERROR] Please refer to E:\maven\web_nanchang\target\surefire-reports for the individual test result ...

  7. 12、Java 正则表达式

    简介 用来描述或者匹配一系列符合某个语句规则的字符串 正则表达式定义了字符串的模式. 正则表达式可以用来搜索.编辑或处理文本. 正则表达式并不仅限于某一种语言,但是在每种语言中有细微的差别. 一.正则 ...

  8. Bytom侧链Vapor源码浅析-节点出块过程

    Bytom侧链Vapor源码浅析-节点出块过程 在这篇文章中,作者将从Vapor节点的创建开始,进而拓展讲解Vapor节点出块过程中所涉及的源码. 做为Vapor源码解析系列的第一篇,本文首先对Vap ...

  9. java多线程(三):多线程单例模式,双重检查,volatile关键字

    一.事先准备 首先准备一个运行用的代码: public class Singleton { public static void main(String[] args) { Thread[] thre ...

  10. JavaScript 实用方法

    1.按时间显示问候语 2.强制光标停留位置 3.保存页面文本 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN& ...