[抄题]:

Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.

Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.

Example:

You may serialize the following tree:

    1
/ \
2 3
/ \
4 5 as "[1,2,3,null,null,4,5]"

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道空节点怎么处理:随便用一个什么字符串代替就行了,反正压缩之后也看不见

[英文数据结构或算法,为什么不用别的数据结构或算法]:

pre-order,写着方便

用deque,先都存了,然后全部先进先出

[一句话思路]:

把空节点提前定义为一个特殊的字符串来处理

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

需要提前定义 spliter = ","

[画图]:

[一刷]:

  1. 节点为空和新建树是两个独立的过程,需要用if else隔开
  2. 反序列化只需要用q做参数即可,因为节点都是recursion中新建的

[二刷]:

  1. 节点为空就是root本身 == null, 不是root.val == null
  2. 写公式就完成recursion了,此时可以return

[三刷]:

  1. 字符串判断相等应该用equals

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

光有死记硬背不行,一点不背想凭空写 更是无稽之谈

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:递归/分治/贪心]:递归

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

271. Encode and Decode Strings就是用stringbuilder就行了

449. Serialize and Deserialize BST 不能中序,因为对递增有要求,输入串未必满足

[代码风格] :

[是否头一次写此类driver funcion的代码] :

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Codec {
//ini: split, null
private static final String split = ",";
private static final String NN = " "; // Encodes a tree to a single string.
public String serialize(TreeNode root) {
//new StringBuilder
StringBuilder sb = new StringBuilder(); //
buildString(root, sb); return sb.toString();
} private void buildString(TreeNode root, StringBuilder sb) {
//if null, else preorder
if (root == null) {
sb.append(NN).append(split);
}else {
sb.append(root.val).append(split);
buildString(root.left, sb);
buildString(root.right, sb);
}
} // Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
//ini: deque
Deque<String> queue = new LinkedList<>(); //put into queue
queue.addAll(Arrays.asList(data.split(","))); return buildTree(queue);
} private TreeNode buildTree(Deque<String> queue) {
String val = queue.remove(); //if null
if (val.equals(NN)) {
return null;
}else {
//else: preoder, return
TreeNode node = new TreeNode(Integer.valueOf(val));
node.left = buildTree(queue);
node.right = buildTree(queue);
return node;
}
}
} // Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));

297. Serialize and Deserialize Binary Tree二叉树的序列化和反序列化(就用Q)的更多相关文章

  1. 297 Serialize and Deserialize Binary Tree 二叉树的序列化与反序列化

    序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据.请设计一个算法来实现二叉树 ...

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

  3. [LeetCode] 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. 【LeetCode】297. Serialize and Deserialize Binary Tree

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

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

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

  9. Leetcode 297. Serialize and Deserialize Binary Tree

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

随机推荐

  1. [LeetCode系列]BST有效性确定问题[前序遍历]

    给定一个BST的根节点, 试判断此BST是否为符合规则的BST? 规则: 对于一个BST的节点, 它左侧的所有节点(包括子节点)必须小于它本身; 它右侧的所有节点(包括子节点)必须大于它本身; 它的左 ...

  2. 如何将angular-ui-bootstrap的分页组件封装成一个指令

    准备工作: (1)一如既往的我还是使用了requireJS进行js代码的编译 (2)必须引入angualrJS , ui-bootstrap-tpls-1.3.2.js , bootstrap.css ...

  3. Linux:WebServer(Nginx 虚拟主机配置与伪静态实现)

    ps + 查看方式  |  grep  +  服务/端口/软件等:查看状态: 一.基本操作 Nginx 多用于商业系统: 一个端口只能被一个服务使用: Nginx 可以同时监听多个端口,也就是配置时, ...

  4. Appium+python自动化23-Android夜神模拟器

    前言 Android SDK虽然也自带了模拟器,但是那速度会让你怀疑人生,并且不稳定经常卡死异常.夜神模拟器可以说是android模拟器里面的一个神器. 环境安装 1.官网下载地址:https://w ...

  5. Java复习——网络编程

    Java从最开始就是支持网络编程的,也正是网络使Java得到发展繁荣.在这里我记录一下如何使用Java进行网络编程,什么是Socket以及Java实现TCP,UDP的编程模型. InetAddress ...

  6. SpringBoot+rest接口+swagger2生成API文档+validator+mybatis+aop+国际化

    代码地址:JillWen_SpringBootDemo mybatis 1. 添加依赖: <dependency> <groupId>org.mybatis.spring.bo ...

  7. spring 声明式事务的坑 @Transactional 注解

    1.首先环境搭建,jar 我就不写了,什么一些spring-core.jar spring-beans.jar spring-content.jar 等等一些包 省略..... 直接上图: sprin ...

  8. svn删除

    [本地删除.然后提交到服务器]与[服务器删除然后本地更新] svn delete svn://路径(目录或文件的全路径) -m “删除备注信息文本” 推荐如下操作: svn delete 文件名 sv ...

  9. Win8电源选项中没有休眠这一项如何让Win8也能够休眠

    我们都知道,Win8默认的电源选项中是没有休眠这一选项的,即使用Alt+F4打开关闭Windows选项窗口也看不到”休眠“.难道Win8就不能够休眠了吗?答案当然不是,我们只要进行一些设置就能让Win ...

  10. 版本控制git之一 - 仓库管理

    git 再开始这个话题之前,让我想起了一件很痛苦的事情,在我大学写毕业论文的时候,我当时的文件是这样保存的 毕业论文_初稿.doc 毕业论文_修改1.doc 毕业论文_修改2.doc 毕业论文_修改3 ...