由于一般的前序遍历不能唯一的还原出原本你的二叉树,所以要改变一下:

记录二叉树的结构信息,也就是空节点用符号表示

一般的前序遍历只是记录了节点的前后顺序,通过记录空节点,每一层的结构就可以记录下来

解码的时候可以按照前序的顺序依次还原节点。

 /*
前序遍历或者层序遍历都可以,前序遍历要保存二叉树的结构信息
空节点用符号表示
*/
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一般二叉树的编解码的更多相关文章

  1. [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 ...

  2. Leetcode 297. Serialize and Deserialize Binary Tree

    https://leetcode.com/problems/serialize-and-deserialize-binary-tree/ Serialization is the process of ...

  3. [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 ...

  4. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  5. LC 297 Serialize and Deserialize Binary Tree

    问题: Serialize and Deserialize Binary Tree 描述: Serialization is the process of converting a data stru ...

  6. [LintCode] Serialize and Deserialize Binary Tree(二叉树的序列化和反序列化)

    描述 设计一个算法,并编写代码来序列化和反序列化二叉树.将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”. 如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将二叉 ...

  7. 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 ...

  8. 【LeetCode】297. Serialize and Deserialize Binary Tree

    二叉树的序列化与反序列化. 如果使用string作为媒介来存储,传递序列化结果的话,会给反序列话带来很多不方便. 这里学会了使用 sstream 中的 输入流'istringstream' 和 输出流 ...

  9. 297. Serialize and Deserialize Binary Tree

    题目: Serialization is the process of converting a data structure or object into a sequence of bits so ...

随机推荐

  1. LeetCode 033 Search in Rotated Sorted Array

    题目要求:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you b ...

  2. 『CDN』让你的网站访问起来更加柔顺丝滑

    我是风筝,公众号「古时的风筝」,一个兼具深度与广度的程序员鼓励师,一个本打算写诗却写起了代码的田园码农! 文章会收录在 JavaNewBee 中,更有 Java 后端知识图谱,从小白到大牛要走的路都在 ...

  3. 以前一个个文件删数据的我,今天终于找到了释放C盘空间的办法

    这是我刚刚清理的C盘,亲测有效!无需安装清理空间的软件,我的电脑品牌是华硕. win10的电脑应该都有搜索功能,如果没有开启,可以鼠标右键点击任务栏. 弹出菜单找到[搜索]-[显示搜索图标],勾选即可 ...

  4. SQL优化之SQL 进阶技巧(上)

    由于工作需要,最近做了很多 BI 取数的工作,需要用到一些比较高级的 SQL 技巧,总结了一下工作中用到的一些比较骚的进阶技巧,特此记录一下,以方便自己查阅,主要目录如下: SQL 的书写规范 SQL ...

  5. 给你一个亿的keys,Redis如何统计?

    前言 不知你大规模的用过Redis吗?还是仅仅作为缓存的工具了?在Redis中使用最多的就是集合了,举个例子,如下场景: 签到系统中,一天对应一系列的用户签到记录. 电商系统中,一个商品对应一系列的评 ...

  6. 如何使用TradingView(TV)回测数字货币交易策略

    更多精彩内容,欢迎关注公众号:数量技术宅.想要获取本期分享的完整策略代码,请加技术宅微信:sljsz01 TradingView平台简介 前段时间,有粉丝找到技术宅,表示他有一个常用的交易平台,叫做T ...

  7. 第15.42节、PyQt输入部件:QFontComboBox、QLineEdit、QTextEdit、QPlainText功能详解

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 一.引言 输入部件量比较多,且功能很丰富,但除了用于编写编辑器.浏览器 ...

  8. PyQt(Python+Qt)学习随笔:QListView的modelColumn属性及困惑

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.modelColumn介绍 QListView的modelColumn属性用于控制视图中展现mo ...

  9. 网鼎杯2020 AreUSerialz

    0x00 前言 ...有一说一,赵总的BUUCTF上的这道题目并没有复现到精髓.其实感觉出题人的题目本身没有那么简单的,只不过非预期实在是太简单惹. 涉及知识点: 1.php中protected变量反 ...

  10. Leetcode学习笔记(1)

    scrapy爬虫的学习告一段落,又因为现在在学习数据结构,做题平台是lettcode:https://leetcode-cn.com/ 每周都要交一次做题的笔记,所以把相关代码和思路同时放在博客上记录 ...