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 ...
随机推荐
- 利用 netsh 给 mysql 开启多端口监听
利用 netsh 给 mysql 开启多端口监听 标题党,实际并不是真的多端口监听,只是端口转发而已. 由于某种特殊原因需要 mysql 服务器多个端口监听. mysql 服务器本身是不支持的,但可以 ...
- ThinkPHP5 为什么取消了单字母函数?
ThinkPHP5 为什么取消了单字母函数? 更容易理解. 理加规范. 个人喜好. 比如 TPShop 也是用 ThinkPHP5 又加回单字母函数. [话唠]教练,我想做菜-长沙 2018/10/8 ...
- How to install torcs package in Debian
Installation instructions Installing torcs package in Debian Wheezy is as easy as running: 没想到在debia ...
- RK3288 dts和dtsi介绍
Device Tree 是一种描述硬件的数据结构,它起源于 OpenFirmware(OF).在 Linux2.6 中,ARM 架构的板机硬件细节过多地被硬编码在 arch/arm/plat-xxx ...
- Ubuntu 14.04 配置安卓5.1编译环境
Ubuntu 14.04版本 电脑cpu必须是64位 硬盘分配大约100G的空间 1.ubuntu中更新源 $ sudo apt-get update 2.android5.1需要安装openjdk- ...
- xdebug : Debug session was finished without being paused
一.当调试模式出现说路径不匹配的时候,需要检查当前请求的URL和设置断点的是否在同样的位置 Debug session was finished without being paused It may ...
- 记录:Web无引用无配置方式动态调用WCF服务
这几年一直用WebApi较多,最近项目中有个需求比较适合使用WCF,以前也用过JQuery直接调用Wcf的,但是说实话真的忘了… 所以这次解决完还是花几分钟记录一下 WCF服务端:宿主在现有Win服务 ...
- C++中const使用注意要点(一)
最近看<C++编程思想>发现自己的基础确实不牢固,也想起了以前写代码时也因为const的事情浪费过时间,这里总结下几个要点. 首先说下内部链接和外部链接. 当一个cpp文件在编译时,预处理 ...
- linux网络编程、系统编程
http://blog.csdn.net/lianghe_work/article/category/2871247
- 《C++ Primer》读书笔记
在C++中,基类必须指出希望派生类重新定义哪些函数,定义为virtual的函数是基类期待派生类重新定义的,基类希望派生类继承的函数不能定义为虚函数. 引用和指针的静态类型与动态类型可以不同,这是C++ ...