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]. 返回如下的树: ...
随机推荐
- API返回延迟,FPM重启后恢复之后又重现 问题解决方案
背景 最近在提供后台API时,提供了一个简单逻辑的接口 部署在测试环境,自测没问题,提交测试 突然有一天,接口响应延迟严重,几乎每次都是3-4秒返回 这对于一个接口来说,肯定是有问题的 于是便有了以下 ...
- Eureka服务发现Discovery
功能: 对于注册进Eureka里面的微服务,可以通过服务发现来获得该服务的信息 修改controller 主启动类加@EnableDiscoveryClient注解
- Mybatis Plus中的lambdaQueryWrapper条件构造图介绍
- centOS7.*安装nginx和简单使用
安装nginx 去官网下载对应的nginx包,推荐使用稳定版本. 上传下载好的包到服务器 安装依赖环境 安装gcc环境. yum install gcc-c++ 安装PCRE库,用于解析正则表达式. ...
- PHP atan() 函数
实例 通过 atan() 函数返回不同数的反正切: <?phpecho(atan(0.50) . "<br>");echo(atan(-0.50) . " ...
- PHP fmod() 函数
实例 返回 x/y 的浮点数余数: <?php$x = 7;$y = 2;$result = fmod($x,$y);echo $result;// $result equals 1, beca ...
- JavaScript正则表达式相关方法
一.正则表达式方法 var str="abcdefabcdef"; (1)reg.test(str); 查看字符串是否有满足正则表达式的内容,并返回一个布尔值true/false ...
- mysql8.0以上版本修改密码问题记录
参考链接: https://blog.csdn.net/qq_27820551/article/details/101488430 https://blog.csdn.net/mukouping82/ ...
- RNN神经网络产生梯度消失和梯度爆炸的原因及解决方案
1.RNN模型结构 循环神经网络RNN(Recurrent Neural Network)会记忆之前的信息,并利用之前的信息影响后面结点的输出.也就是说,循环神经网络的隐藏层之间的结点是有连接的,隐藏 ...
- Cesium加载倾斜摄影数据
(1)倾斜摄影数据仅支持 smart3d 格式的 osgb 组织方式, 数据目录必须有一个 “Data” 目录的总入口, “Data” 目录同级放置一个 metadata.xml 文件用来记录模型的位 ...