[leetcode]297. Serialize and Deserialize Binary Tree一般二叉树的编解码
由于一般的前序遍历不能唯一的还原出原本你的二叉树,所以要改变一下:
记录二叉树的结构信息,也就是空节点用符号表示
一般的前序遍历只是记录了节点的前后顺序,通过记录空节点,每一层的结构就可以记录下来
解码的时候可以按照前序的顺序依次还原节点。
/*
前序遍历或者层序遍历都可以,前序遍历要保存二叉树的结构信息
空节点用符号表示
*/
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)
{
//#代表空节点
s.append("#");
s.append(",");
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[] d = data.split(",");
//用一个链表记录节点,在调用函数中对链表进行改变是会改变堆中真正的链表的
LinkedList<String> list = new LinkedList<>();
for (String s :
d) {
list.add(s);
}
return dehelper(list);
}
public TreeNode dehelper(LinkedList<String> list)
{
//pollfirst和poll的区别是,前者在为空时会返回null
String s = list.pollFirst();
if (s==null||s.equals("#")) return null;
//按照前序遍历的顺序,依次把节点放回去
TreeNode cur = new TreeNode(Integer.parseInt(s));
cur.left = dehelper(list);
//注意下边这list和上边的list已经不一样了,因为在上边函数中改变了
// 虽然传入的是list变量的副本,但是原本和副本都是指向一个地址,都会造成改变
cur.right = dehelper(list);
return cur;
}
[leetcode]297. Serialize and Deserialize Binary Tree一般二叉树的编解码的更多相关文章
- [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 ...
- Leetcode 297. Serialize and Deserialize Binary Tree
https://leetcode.com/problems/serialize-and-deserialize-binary-tree/ Serialization is the process of ...
- [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 ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- LC 297 Serialize and Deserialize Binary Tree
问题: Serialize and Deserialize Binary Tree 描述: Serialization is the process of converting a data stru ...
- [LintCode] Serialize and Deserialize Binary Tree(二叉树的序列化和反序列化)
描述 设计一个算法,并编写代码来序列化和反序列化二叉树.将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”. 如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将二叉 ...
- LeetCode OJ 297. Serialize and Deserialize Binary Tree
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree
二叉树的序列化与反序列化. 如果使用string作为媒介来存储,传递序列化结果的话,会给反序列话带来很多不方便. 这里学会了使用 sstream 中的 输入流'istringstream' 和 输出流 ...
- 297. Serialize and Deserialize Binary Tree
题目: Serialization is the process of converting a data structure or object into a sequence of bits so ...
随机推荐
- golang 自学系列(三)—— if,for,channel
golang 自学系列(三)-- if,for,channel 一般情况下,if 语句跟大多数语言的 if 判断语句一样,根据一个 boolean 表达式结果来执行两个分支逻辑. 但凡总是有例外,go ...
- kali修改配置文件后并保存
vim是进入配置文件并修改 修改完按Esc进入控制模式,再:w保存 :wq是保存并退出
- vue回调接口
1.微博回调接口 1.1oauth/urls.py 中添加路由 urlpatterns = [ path('weibo/callback/', views.OauthWeiboCallback.as_ ...
- MySQL索引(二):建索引的原则
在了解了索引的基础知识及B+树索引的原理后(如需复习请点这里),这一节我们了解一下有哪些建索引的原则,来指导我们去建索引. 建索引的原则 1. 联合索引 我们可能听一些数据库方面的专业人士说过:&qu ...
- Python中判断一个中文是否中文数字的方法
Python内置功能非常强大,在字符串内置函数中提供了一个判断字符串是否全数字的方法,而且这个方法不只是简单判断阿拉伯数字,包括中文数字和全角的阿拉伯数字都认识,这个函数就是字符串的isnumeric ...
- PyQt(Python+Qt)学习随笔:QTableWidget中表格各列平均分配宽度的两种方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 在QTableWidget表格部件中,如果需要实现表格中各列要占满部件中的可用空间,同时实现各列平分 ...
- PyQt(Python+Qt)学习随笔:QListView的uniformItemSizes属性
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QListView的uniformItemSizes属性用于控制视图中所有项是否具有相同大小,只有在 ...
- 学习JUC源码(1)——AQS同步队列(源码分析结合图文理解)
前言 最近结合书籍<Java并发编程艺术>一直在看AQS的源码,发现AQS核心就是:利用内置的FIFO双向队列结构来实现线程排队获取int变量的同步状态,以此奠定了很多并发包中大部分实现基 ...
- 对象存储COS全球加速助力企业出海
近年来,中国互联网行业迅猛发展,国内庞大的市场孕育出了许多现象级的产品,也锤炼出了非常成熟的产业链.与此同时,很多海外市场还处于萌芽期,存在着巨大的流量红利,越来越多的互联网企业开始加速"出 ...
- 题解-洛谷P5410 【模板】扩展 KMP(Z 函数)
题面 洛谷P5410 [模板]扩展 KMP(Z 函数) 给定两个字符串 \(a,b\),要求出两个数组:\(b\) 的 \(z\) 函数数组 \(z\).\(b\) 与 \(a\) 的每一个后缀的 L ...