[leetcode]449. Serialize and Deserialize BST设计BST的编解码
这道题学到了东西。
/*
一开始想着中序遍历,但是解码的时候才发现,中序遍历并不能唯一得确定二叉树。
后来看了网上的答案,发现先序遍历是可以的,观察了一下,对于BST,先序遍历确实是可以
唯一得确定。
对于一般的二叉树,必须是前序和中序或者后序和中序才能唯一确定,其中中序的作用就是
确定中心位置,以确定左右子树的前序(后序)的范围
但是对于BST根据性质,左子树的前序序列最大的就是1st节点,所以找到序列中第一个比
1st节点大的节点就是右子树的开始,这样就不需要中序了
还有一种方法,保存二叉树的结构信息,这种做法可以应用于任何二叉树。保存结构信息就是空节点添加一个符号代替,解码的时候就知道结构信息了
*/
//记录数据
StringBuilder s = new StringBuilder();
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
preTra(root);
return new String(s);
}
//前序遍历
public void preTra(TreeNode root)
{
if (root==null) return;
s.append(root.val);
s.append(",");
preTra(root.left);
preTra(root.right);
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if (data.length()==0) return null;
String[] vals = data.split(",");
return helper(vals,0,vals.length-1);
}
//解码过程,和由前序及中序得到二叉树的过程类似
public TreeNode helper(String[] data,int sta,int end)
{
if (sta > end) return null;
//根据BST的特点,获得左右子树前序遍历序列的起止点
int rightSta = rSta(data,sta,end);
TreeNode cur = new TreeNode(Integer.parseInt(data[sta]));
cur.left = helper(data,sta+1,rightSta-1);
cur.right = helper(data,rightSta,end);
return cur;
}
public int rSta(String[] data,int sta,int end)
{
/*
第一个比根节点大的值就是右子树的开始
*/
int a = Integer.parseInt(data[sta]);
while (sta<=end)
{
if (Integer.parseInt(data[sta])>a)
break;
sta++;
}
return sta;
}
对于普通二叉树,前序(后序)+中序可以唯一确定二叉树,前序(后序)包含父子关系信息,中序包含兄弟关系信息
下边是前序+中序确定二叉树
/*
* 如果知道(中序遍历和前序遍历)或者(中序遍历和后序遍历)可以唯一确定一棵树*/
/*
由前序遍历和中序遍历确定二叉树
*/
public TreeNode PaI(int[] pre,int[] in)
{
if (pre.length==0) return null;
//只剩下一个节点,就直接返回这个节点
if (pre.length==1)
return new TreeNode(pre[0]);
//根据前序遍历的开头获得中序遍历的中心
int center = pre[0];
int c = 0;
for (int i = 0; i < in.length; i++) {
if (in[i]==center)
{
c = i;
break;
}
}
//根据中序遍历和中心位置确定左右子树的前中序遍历序列,递归求子树
//左右子树的中序遍历就是中心位置的左右子序列
//左右子树的前序遍历序列是根节点后边序列分为的两部分,长度分别与其中序遍历序列长度一样
TreeNode cur = new TreeNode(center);
cur.left = PaI(Arrays.copyOfRange(pre,1, 1 + c),Arrays.copyOfRange(in,0,c));
cur.right = PaI(Arrays.copyOfRange(pre,c+1,pre.length),Arrays.copyOfRange(in,c+1,in.length));
return cur;
}
步骤记住两点:所有节点信息都是从前序序列中获得(每次都是根据前序的1st节点信息作为根节点),中序遍历只是用来确定左右子树的子前序的起止点。
然后递归获得左右子树
[leetcode]449. Serialize and Deserialize BST设计BST的编解码的更多相关文章
- [leetcode]449. Serialize and Deserialize BST序列化与反序列化BST
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [leetcode]449. Serialize and Deserialize BST序列化反序列化二叉搜索树(尽量紧凑)
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- LeetCode 449. Serialize and Deserialize BST
原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ 题目: Serialization i ...
- 【LeetCode】449. Serialize and Deserialize BST 解题报告(Python)
[LeetCode]449. Serialize and Deserialize BST 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/pro ...
- 【leetcode】449. Serialize and Deserialize BST
题目如下: Serialization is the process of converting a data structure or object into a sequence of bits ...
- 449. Serialize and Deserialize BST
https://leetcode.com/problems/serialize-and-deserialize-bst/#/description Serialization is the proce ...
- 449. Serialize and Deserialize BST——几乎所有树的面试题目都会回到BFS或者DFS,使用BFS,None节点存#
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- LeetCode 428. Serialize and Deserialize N-ary Tree
原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-n-ary-tree/ 题目: Serialization is the ...
- [leetcode]297. Serialize and Deserialize Binary Tree 序列化与反序列化二叉树
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
随机推荐
- Beta冲刺随笔——Day_Five
这个作业属于哪个课程 软件工程 (福州大学至诚学院 - 计算机工程系) 这个作业要求在哪里 Beta 冲刺 这个作业的目标 团队进行Beta冲刺 作业正文 正文 其他参考文献 无 今日事今日毕 林涛: ...
- shiro利用过期时间,解决用户冻结踢出问题
背景 shiro中需要冻结某个用户,但是此时此刻这个用户在线,如果冻结只是改变状态的话,只会导致用户不满,所以要改变这个办法. 在查找过程中发现都是告诉shiro写自定义过滤器,那么我如果自定义过滤器 ...
- DockerFile理解与应用
1.DockerFile是什么? DockerFile是用来构建Docker镜像的构建文件,一般分为四部分:基础镜像信息.维护者信息.镜像操作指令和容器启动时执行指令,'#' 为 Dockerfile ...
- 第14.10节 Python中使用BeautifulSoup解析http报文:html标签相关属性的访问
一. 引言 在<第14.8节 Python中使用BeautifulSoup加载HTML报文>中介绍使用BeautifulSoup的安装.导入和创建对象的过程,本节介绍导入后利用Beauti ...
- PyQt学习随笔:Qt中tem Views(Model-Based)和Item Widgets(Item-Based)控件的用途和关系
在界面程序开发中,数据的展示主要包括表格.简单列表.树状列表以及纯文本等多种方式,在Qt中将界面表格.简单列表.树状列表称为"表项视图类(item view class)",并提供 ...
- PyQt(Python+Qt)学习随笔:Qt Designer中toolBar的toolButtonStyle属性
tooButtonStyle属性保存主工具栏按钮的样式设置,用来表示工具栏按钮的文字和图标怎么显示. 该属性的可设置值类型为枚举类型Qt.ToolButtonStyle,它包含如下值: 该属性的缺省值 ...
- js2py 的用法
python调用js的方法 js2py的简单用法 import js2py js = """ function add(a, b) { return a + b } &q ...
- Asp.NetCore之AutoMapper进阶篇
应用场景 在上一篇文章--Asp.NetCore之AutoMapper基础篇中我们简单介绍了一些AutoMapper的基础用法以及如何在.NetCore中实现快速开发.我相信用过AutoMapper实 ...
- java中==和equals的不同使用方法
System.out.println("input a charact a "); Scanner input2 = new Scanner(System.in); St ...
- hash相关
转译☞:https://www.cs.rice.edu/~as143/COMP441_Spring17/scribe/lect4.pdf 1 大规模图片检索问题 基于树模型的算法在分类跟聚类中很受欢迎 ...