Java数据结构——根据遍历结果构造二叉树
一、已知前序、中序、后序遍历结果的其中两种,还原二叉树。
①已知前序遍历结果: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数据结构——根据遍历结果构造二叉树的更多相关文章
- Java实现 LeetCode 106 从中序与后序遍历序列构造二叉树
106. 从中序与后序遍历序列构造二叉树 根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序 ...
- Java实现 LeetCode 105 从前序与中序遍历序列构造二叉树
105. 从前序与中序遍历序列构造二叉树 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中 ...
- lintcode: 中序遍历和后序遍历树构造二叉树
题目 中序遍历和后序遍历树构造二叉树 根据中序遍历和后序遍历树构造二叉树 样例 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下的树: 2 / \ 1 3 注意 你可 ...
- LeetCode---105. 从前序与中序遍历序列构造二叉树 (Medium)
题目:105. 从前序与中序遍历序列构造二叉树 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7 ...
- lintcode :前序遍历和中序遍历树构造二叉树
解题 前序遍历和中序遍历树构造二叉树 根据前序遍历和中序遍历树构造二叉树. 样例 给出中序遍历:[1,2,3]和前序遍历:[2,1,3]. 返回如下的树: 2 / \ 1 3 注意 你可以假设树中不存 ...
- 【2】【leetcode-105,106】 从前序与中序遍历序列构造二叉树,从中序与后序遍历序列构造二叉树
105. 从前序与中序遍历序列构造二叉树 (没思路,典型记住思路好做) 根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [ ...
- [leetcode]从中序与后序/前序遍历序列构造二叉树
从中序与后序遍历序列构造二叉树 根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 po ...
- LintCode-72.中序遍历和后序遍历树构造二叉树
中序遍历和后序遍历树构造二叉树 根据中序遍历和后序遍历树构造二叉树 注意事项 你可以假设树中不存在相同数值的节点 样例 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下的树: ...
- LintCode-73.前序遍历和中序遍历树构造二叉树
前序遍历和中序遍历树构造二叉树 根据前序遍历和中序遍历树构造二叉树. 注意事项 你可以假设树中不存在相同数值的节点 样例 给出中序遍历:[1,2,3]和前序遍历:[2,1,3]. 返回如下的树: ...
随机推荐
- Day02_SpringCloud
学于黑马和传智播客联合做的教学项目 感谢 黑马官网 传智播客官网 微信搜索"艺术行者",关注并回复关键词"乐优商城"获取视频和教程资料! b站在线视频 0.学习 ...
- PHP is_dir() 函数
定义和用法 is_dir() 函数检查指定的文件是否是一个目录. 如果目录存在,该函数返回 TRUE. 语法 is_dir(file) 参数 描述 file 必需.规定要检查的文件. 提示和注释 注释 ...
- PHP atan() 函数
实例 通过 atan() 函数返回不同数的反正切: <?phpecho(atan(0.50) . "<br>");echo(atan(-0.50) . " ...
- PHP xml_set_processing_instruction_handler() 函数
定义和用法 xml_set_processing_instruction_handler() 函数规定当解析器在 XML 文档中找到处理指令时被调用的函数. 处理指令包含在 <? 和 ?> ...
- CMD使用笔记
CMD杂谈 基本功: 1,列出所有任务及进程号,杀进程 tasklist tasklist /? 获取使用帮助 taskkill taskkill /? 获取使用帮助 2,cd 切换目录 cd ...
- Linux的VMWare中Centos7的安装
Windows平台下VMWare 14安装Centos 7 一.虚拟机硬件配置 1.选择创建新的虚拟机: 2.选择自定义(高级)进行自定义配置,单击下一步: 3.选择虚拟机硬件兼容性为默认,单击下一步 ...
- 使用HttpClient 发送 GET、POST(FormData、Raw)、PUT、Delete请求及文件上传
httpclient4.3.6 package org.caeit.cloud.dev.util; import java.io.File; import java.io.IOException; i ...
- spring boot项目集成zuul网关
1 zuul简介 Zuul 的官方介绍是 “Zuul is the front door for all requests from devices and web sites to the back ...
- MySQL回顾
一. 对数据库的操作 1. 创建一个库 create database 库名 create database 库名 character set 编码 创建带有编码的 查看编码: 2. 删除一个库 dr ...
- 曲线生成与求交—Bezier曲线
Bezier曲线生成 法国工程师Pierre Bezier在雷诺公司使用该方法来设计汽车.一条Bezier曲线可以拟合任何数目的控制点. 公式 设\(n+1\)个控制点\(P_0,P_1--P_n\) ...