297. Serialize and Deserialize Binary Tree二叉树的序列化和反序列化(就用Q)
[抄题]:
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 = ","
[画图]:

[一刷]:
- 节点为空和新建树是两个独立的过程,需要用if else隔开
- 反序列化只需要用q做参数即可,因为节点都是recursion中新建的
[二刷]:
- 节点为空就是root本身 == null, 不是root.val == null
- 写公式就完成recursion了,此时可以return
[三刷]:
- 字符串判断相等应该用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)的更多相关文章
- 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] 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 ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree
二叉树的序列化与反序列化. 如果使用string作为媒介来存储,传递序列化结果的话,会给反序列话带来很多不方便. 这里学会了使用 sstream 中的 输入流'istringstream' 和 输出流 ...
- [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 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
https://leetcode.com/problems/serialize-and-deserialize-binary-tree/ Serialization is the process of ...
随机推荐
- python编程规范系列--建议01~07
本系列来自<编写高质量代码 改善python程序的91个建议>的读书笔记整理. 本书主要内容 1)容易被忽视的重要概念和常识,如代码的布局和编写函数的原则等: 2)编写py ...
- 【常见Web应用安全问题】---4、Directory traversal
Web应用程序的安全性问题依其存在的形势划分,种类繁多,这里不准备介绍所有的,只介绍常见的一些. 常见Web应用安全问题安全性问题的列表: 1.跨站脚本攻击(CSS or XSS, Cross Si ...
- springMvc架构简介
什么是spring 关于spring的定义无论是从官方还是市面上已经很多能够清晰明了的做出解释了.我姑且简单定义它为一个轻量级的控制反转(IoC)和面向切面(AOP)的容器,Java 开发框架,至于控 ...
- Ten Qualities of an Effective Team Player
If you were choosing team members for a business team in your organization, who would the best team ...
- java代码--------实现位运算符不用乘除法啊
总结:<<:乘法 >>:除法 package com.mmm; public class dfd { public static void main(String[] args ...
- 【转】Hadoop学习路线图
按照这个路线图来学习即可. 1.M. Tim Jones的三篇文章: 用Hadoop进行分布式数据处理第1部分(入门):http://www.ibm.com/developerworks/ ...
- 一次hadoop集群机器加内存的运维过程
由于前期的集群规划问题,导致当前Hadoop集群中的硬件并没有完全利用起来.当前机器的内存CPU比例为2G:1core,但一般的MapReduce任务(数据量处理比较大,逻辑较复杂)的MR两端都需要将 ...
- Linux系统启动流程与系统目录
启动流程 # 1,开机BIOS自检 检查一系列的硬件,最后根据启动顺序启动,是硬盘还是光驱等 # 2,MBR引导 读硬盘0柱面0磁道1扇区的前446字节 1扇区=512字节 MBR=4446字节 四个 ...
- 10 删除topic中的数据
1 打开 server.properties2 添加一条: delete.topic.enable=true 3 执行命令: bin/kafka-topics.sh --delete ...
- Nginx加状态监控
安装Nginx时加上 –with-http_stub_status_module 在nginx.conf server location /nginx_status { stub_sta ...