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

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

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

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

 /*
前序遍历或者层序遍历都可以,前序遍历要保存二叉树的结构信息
空节点用符号表示
*/
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. [整理]qbxt集训10场考试 大 杂 烩 (前篇)

    Contest 1 A 计算 \(n!\mod 2^{32}\) .发现数一大答案就为 \(0\) ,直接输出即可. B 一个 \(n\times m\) 的网格,网格中的数都在 \([1,nm]\) ...

  2. 【常见踩坑】】USB调试安装失败(Installation failed with message INSTALL_CANCELED_BY_USER)

    [参考]http://www.cnblogs.com/liushilin/p/6553918.html 问题:在USB安装调试(小米手机),出现如下错误 解决:1.小米手机解决办法见参考.登录小米账号 ...

  3. PyQt(Python+Qt)学习随笔:纯文本编辑器QPlainTextEdit功能详解

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 一.概述 QPlainTextEdit是用于纯文本的一个高级文档编辑器 ...

  4. 第11.23节 Python 中re模块的搜索替换功能:sub及subn函数

    一. 引言 在<第11.3节 Python正则表达式搜索支持函数search.match.fullmatch.findall.finditer>重点介绍了几个搜索函数,除了搜索,re模块也 ...

  5. PyQt(Python+Qt)学习随笔:设定toolButton弹出菜单的方法

    在Qt Designer中toolButton可以通过popupMode设定菜单弹出的模式,但并不能在Qt Designer中指定toolButton的弹出菜单,toolButton只能通过代码来指定 ...

  6. 关于我 About Me

    重庆某大学计算机专业大三学渣 CTF酱油选手 web安全菜鸡 SRC低危小子 精通多门语言 hello world 输出 和 windows linux单词拼写 扣扣:MjU4NTYxNDQ2NA== ...

  7. django学习——request.POST.get(‘key’) 、 request.GET.get('key', '')

    request.POST是用来接受从前端表单中传过来的数据,比如用户登录过程中传递过来的username.passwrod等字段.返回类型是字典: 在后台进行数据获取时,有两种方法(以username ...

  8. JS "&&"操作符妙用

    首先来了解一下 "&&"操作符的工作原理: "&&"连接两个表达式,当两侧表达式都为真时,返回TRUE.有一个为假则返回FALS ...

  9. demo集合

    demos的github地址:https://github.com/zy0628/demos 包含以下demo: 1.图片上传.编辑.合成一张图 需求:1.1.拍照或上传本地相册. 2.2.可以截取图 ...

  10. 双端口RAM和多模块存储器

    目录 双端口RAM 存取周期 双端口RAM 多模块存储器 普通存储器 单体多字存储器 多体并行的存储器 高位交叉编址的多体存储器 低位交叉编址的多提存储器 为什么要这么弄? 高位 低位 流水线(考试常 ...