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

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

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

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

 /*
前序遍历或者层序遍历都可以,前序遍历要保存二叉树的结构信息
空节点用符号表示
*/
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. Spring中的Mybatis

    1. 前言 在构建一个web应用时基本的套路就是SSM,其中的M就是Mybatis. Mybatis作为一款开源的ORM框架, 由于其易于上手的特点成为当下比较流行的ORM框架,当然它还有一款插件能够 ...

  2. 08_UI控件

    uiControl整体界面如下图所示,按照视频教程,学习控件由于是初学,都是最基础知识.还有ImageSwitcher.Gallery未更新,o(╯□╰)o 1 package com.example ...

  3. crash安装使用

     cash作为Linux内核调试的工具是必不可少少的一部分,但是他的下载并不是 yum install一下这么简单的,本文就来讲一下如何安装crash进行调试.  首先就是了解Linux的内核版本.这 ...

  4. 日志聚合工具之 Loki

    本文使用的 Loki 和 Promtail 版本为 1.6.1,Grafana 版本为 7.2.0:部署在 Linux 服务器 Loki 负责日志的存储和查询:Promtail 负责日志的采集并推送给 ...

  5. 在html页中添加视频的几种方式

    1.avi格式代码片断如下: <object id="video" width="400" height="200" border=& ...

  6. 常见SQL注入点判断

    sql注入手工检测 SQL注入手工检测 1基本检测 数字型 字符型 搜索型 POST注入 布尔盲注 报错注入 堆叠注入 判断是什么数据库 2绕过技巧 大小写 替换关键字 使用编码 注释和符号 等价函数 ...

  7. linux 复制文件前n行到另外一个文件

    head -n 100 train.txt > 123.txt head -100 train.txt > 123.txt

  8. 3、tensorflow变量运算,数学运算

    import tensorflow as tf import numpy as np a = tf.range(1,7) a = tf.reshape(a,[2,3]) b = tf.constant ...

  9. 6、Sping Cloud Feign

    1.Spring Cloud Feign简介 (1).Fegin简介 官方文档:http://projects.spring.io/spring-cloud/spring-cloud.html#spr ...

  10. 【题解】「P6832」[Cnoi2020]子弦

    [题解]「P6832」[Cnoi2020]子弦第一次写月赛题解( 首先第一眼看到这题,怎么感觉要用 \(\texttt{SAM}\) 什么高科技的?结果一仔细读题,简单模拟即可. 我们不难想出,出现最 ...