[leetcode]449. Serialize and Deserialize BST序列化与反序列化BST
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 search tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary search tree can be serialized to a string and this string can be deserialized to the original tree structure.
The encoded string should be as compact as possible.
Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.
思路
To makes it the most compact possible, since we just need the order the values were inserted, we do not need to account for null nodes in the string with "#" or "null".
Hence, the final string contains only the values and separators, which makes it the most compact possible.
1. 在encode时候,don't account for null nodes
2. 在decode时候,不再用queue来存整个treenode的信息,而是用大小为1的数组idx来track当前扫到哪一个node了, 用idx来拿到该node value值(即mock up a pass-by-address process)。通过BST的attribute (left < root < right)来build tree.
代码
public class Codec { // Encodes a tree to a single string. public String serialize(TreeNode root) { StringBuilder sb = new StringBuilder(); buildString(root, sb); return sb.toString(); } private void buildString(TreeNode root, StringBuilder sb) { if (root == null) return; sb.append(root.val).append(","); buildString(root.left, sb); buildString(root.right, sb); } // Decodes your encoded data to tree. public TreeNode deserialize(String data) { if(data.length() == 0) return null; // use idx to mockup a pass-by-address int[] idx = new int[]{0}; return buildTree(data.split(","), idx, Integer.MIN_VALUE, Integer.MAX_VALUE); } private TreeNode buildTree(String[] strArr, int[] idx, int min, int max) { if (idx[0] == strArr.length) return null; int val = Integer.parseInt(strArr[idx[0]]); // Go back if out of boundary if (val < min || val > max) return null; TreeNode root = new TreeNode(val); // move to next address idx[0]++; // corresponding to encode pre-order root.left = buildTree(strArr, idx, min, val); root.right = buildTree(strArr, idx, val, max); return root; } }
[leetcode]449. Serialize and Deserialize BST序列化与反序列化BST的更多相关文章
- [leetcode]449. Serialize and Deserialize BST序列化反序列化二叉搜索树(尽量紧凑)
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- LeetCode 449. Serialize and Deserialize BST
原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ 题目: Serialization i ...
- [leetcode]449. Serialize and Deserialize BST设计BST的编解码
这道题学到了东西. /* 一开始想着中序遍历,但是解码的时候才发现,中序遍历并不能唯一得确定二叉树. 后来看了网上的答案,发现先序遍历是可以的,观察了一下,对于BST,先序遍历确实是可以 唯一得确定. ...
- 【LeetCode】449. Serialize and Deserialize BST 解题报告(Python)
[LeetCode]449. Serialize and Deserialize BST 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/pro ...
- 【leetcode】449. Serialize and Deserialize BST
题目如下: Serialization is the process of converting a data structure or object into a sequence of bits ...
- 449. Serialize and Deserialize BST
https://leetcode.com/problems/serialize-and-deserialize-bst/#/description Serialization is the proce ...
- [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]297. Serialize and Deserialize Binary Tree 序列化与反序列化二叉树
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- 449. Serialize and Deserialize BST——几乎所有树的面试题目都会回到BFS或者DFS,使用BFS,None节点存#
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
随机推荐
- mongo aggregate 用法记录
mongo 聚合查询查询还是很方便的,做下记录 依赖的jar是org.springframework.data.mongodb 1.9.6 低版本可能不支持. 数据结构 大概是 这是一份 ...
- css3 之border-radius 属性解析
在css 设置样式的时候,有时候会用到将元素的边框设置为圆形的样子的时候,一般都是通常直接设置:{border-radius:5px },这样就行了,但是到底是什么意思,一直以来都没有弄明白,只是知道 ...
- 6.5 Shell 算术计算
6.5 Shell Arithmetic shell允许在其内计算表达式,可以通过以下方式使用:((中,let和带-i选项的declare命令中. 只能计算固定长度的整数,而且不会检查溢出,除0可以捕 ...
- ReactiveX 学习笔记(11)对 LINQ 的扩展
Interactive Extensions(Ix) 本文的主题为对 Ix 库,对 LINQ 的扩展. Buffer Ix.NET Buffer Ix.NET BufferTest Buffer 方法 ...
- 21 week4 submit buidAndRun() node-rest-client
. 我们想实现一个提交代码的功能 这个功能有nodeserver 传到后边的server 验证 在返回给nodeserver 我们稍微修改一下ui ATOM修改文件权限不够 用下面命令 我们 Cont ...
- AngularJS理论知识
两个核心概念 三个架构 MVC 一切应用程序都是数据的增删改查 那么总要有东西装数据吧 Model就是干这个事(数据表现和操作) View(展现数据) Controller(逻辑) 那么M- V- C ...
- 浏览器唤起APP的思路(本文转载)
在做 h5 页面中,会遇到这样一个需求,有一个立即打开的按钮,如果本地安装了我们的 app,那么点击就直接唤起本地 app,如果没有安装,则跳转到下载. 首先想到的是两个问题:一是如何唤起本地 app ...
- css3选择器补充
一.关系选择器 1.E+F (E元素下一个满足条件的兄弟元素节点) <style> div + p{ background-color:red;// 第一个p元素变色 } </s ...
- HTML css 样式表
CSS样式表 2.1.样式表的基本概念 2.1.1.样式表分类 1.内联样式表 和html联合显示,控制精确,但是可重用性差,冗余多. 例:<p style="font-size:14 ...
- ADO.Net 数据库 删除
删除数据库里的信息和之前增加,修改大同小异,其写法更加简单,也是把SQL语句写为删除语句 删除一条数据,只需要获取并接收到这条数据唯一的能够代表这条数据的信息,比如主键 代码演示: using Sys ...