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.

题目

序列化反序列化二叉搜索树(尽量紧凑)

思路

这个题最大不同是 “encoded string needs to be as compact as possible”. 即encode尽量紧凑

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.

那么我们做的一个优化是,

在encode的时候,不再将root == null的信息encode到string里

问题来了,在decode的时候,怎么读取root == null的结点信息呢?

用int[] idx = new int[]{0}去track当前处理的是String[] nodes 中的第几个node

利用BST的attribute

若 such node value < min || > max, 肯定越界,返null

代码

 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;
/*Q: 为何不将 root == null 加到stringbuilder里?
Since we just need the order the values were inserted, you 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.
Q: 为何选择pre-order
preorder traversal 的时候, 对于当前节点,所有的左节点比他小,右节点比他大
*/
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;
//
int[] idx = new int[]{0};
idx[0] = 0;
return buildTree(data.split(","), idx, Integer.MIN_VALUE, Integer.MAX_VALUE);
} private TreeNode buildTree(String[] nodes, int[] idx, int min, int max) {
if (idx[0] == nodes.length) return null;
int val = Integer.parseInt(nodes[idx[0]]);
if (val < min || val > max) return null; // Go back if we are over the boundary
TreeNode cur = new TreeNode(val);
idx[0]++; // update current position
/*keep with encode pre-order*/
cur.left = buildTree(nodes, idx, min, val);
cur.right = buildTree(nodes, idx, val, max);
return cur;
}
}

[leetcode]449. Serialize and Deserialize BST序列化反序列化二叉搜索树(尽量紧凑)的更多相关文章

  1. [leetcode]449. Serialize and Deserialize BST序列化与反序列化BST

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  2. LeetCode 449. Serialize and Deserialize BST

    原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ 题目: Serialization i ...

  3. 449 Serialize and Deserialize BST 序列化和反序列化二叉搜索树

    详见:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ C++: /** * Definition fo ...

  4. [leetcode]449. Serialize and Deserialize BST设计BST的编解码

    这道题学到了东西. /* 一开始想着中序遍历,但是解码的时候才发现,中序遍历并不能唯一得确定二叉树. 后来看了网上的答案,发现先序遍历是可以的,观察了一下,对于BST,先序遍历确实是可以 唯一得确定. ...

  5. 【LeetCode】449. Serialize and Deserialize BST 解题报告(Python)

    [LeetCode]449. Serialize and Deserialize BST 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/pro ...

  6. Java实现 LeetCode 449 序列化和反序列化二叉搜索树

    449. 序列化和反序列化二叉搜索树 序列化是将数据结构或对象转换为一系列位的过程,以便它可以存储在文件或内存缓冲区中,或通过网络连接链路传输,以便稍后在同一个或另一个计算机环境中重建. 设计一个算法 ...

  7. 【leetcode-449】序列化和反序列化二叉搜索树

    序列化是将数据结构或对象转换为一系列位的过程,以便它可以存储在文件或内存缓冲区中,或通过网络连接链路传输,以便稍后在同一个或另一个计算机环境中重建. 设计一个算法来序列化和反序列化二叉搜索树. 对序列 ...

  8. 【leetcode】449. Serialize and Deserialize BST

    题目如下: Serialization is the process of converting a data structure or object into a sequence of bits ...

  9. 449. Serialize and Deserialize BST

    https://leetcode.com/problems/serialize-and-deserialize-bst/#/description Serialization is the proce ...

随机推荐

  1. iOS 证书, provision profile作用

    证书(certificate): 给app签名用的,针对开发者,app可以装在真机上的前提条件之一是被签名 Provision profile: 在app包中,用来校验app是否可以被装在真机上,一个 ...

  2. 尚硅谷springboot学习6-eclipse创建springboot项目的三种方法(转)

    方法一 安装STS插件 安装插件导向窗口完成后,在eclipse右下角将会出现安装插件的进度,等插件安装完成后重启eclipse生效 新建spring boot项目 项目启动 方法二 1.创建Mave ...

  3. Haskell语言学习笔记(79)lambda演算

    lambda演算 根据维基百科,lambda演算(英语:lambda calculus,λ-calculus)是一套从数学逻辑中发展,以变量绑定和替换的规则,来研究函数如何抽象化定义.函数如何被应用以 ...

  4. 转: python requests的安装与简单运用

    requests是Python的一个HTTP客户端库,跟urllib,urllib2类似,那为什么要用requests而不用urllib2呢? 官方文档中是这样说明的: python的标准库urlli ...

  5. Notepad++好用的功能和插件

    Notepad++是一款Windows环境下免费开源的代码编辑器,支持Python,shell,Java等主流语言编写.本文主要描述Notepad++一些好用但是容易忽视的功能. 1.根据文件内容查找 ...

  6. Python中fileinput模块使用方法

    fileinput模块提供处理一个或多个文本文件的功能,可以通过使用for循环来读取一个或多个文本文件的所有行.python2.7文档关于fileinput介绍:fileinput   fileinp ...

  7. 处理TypeError: Converting circular structure to JSON

    // Demo: Circular reference var o = {}; o.o = o; // Note: cache should not be re-used by repeated ca ...

  8. 8.mysql-基础.md

    目录 数据库管理 查看当前软件中的数据库 工具 创建数据库 删除数据 查看字符集 修改数据库 表管理 进入数据库 查看表 创建表 查看表结构 删除表 修改表 添加字段 删除字段 修改字段名称 修改字段 ...

  9. eclipse maven项目 热部署

    热部署:本地项目一键发布到远程服务器中 热部署步骤: 1. 在tomat/conf/tomcat-users.xml添加 <role rolename="manager-gui&quo ...

  10. jenkins 自动触发

    在gitlab上配置连接jenkins ,将Jenkins的Secret token 与Build URL 复制到gitlab中 在settings标签下面,找到OutBound Request,勾选 ...