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的更多相关文章

  1. [leetcode]449. Serialize and Deserialize 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. [leetcode]449. Serialize and Deserialize BST设计BST的编解码

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

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

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

  5. 【leetcode】449. Serialize and Deserialize BST

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

  6. 449. Serialize and Deserialize BST

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

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

随机推荐

  1. 配置maven访问nexus,配置项目pom.xml以发布maven项目到nexus中

    maven访问nexus有三种配置方法,分别为: 项目pom.xml,优先级最高: user的settings.xml,优先级中,未在pom.xml中配置repository标签,则使用这个配置: m ...

  2. jquery接触初级----jquery 选择器

    css 选择器主要有:元素选择器,ID选择器,类选择器,群组选择器,后代选择器,普通配符选择器等,通过css选择,我们可以很方便的给元素添加样式,使网页看起来更加好看 jquery 选择器也有相似的功 ...

  3. BerOS File Suggestion(stl-map应用)

    Polycarp is working on a new operating system called BerOS. He asks you to help with implementation ...

  4. 尚硅谷springboot学习21-web开发-处理静态资源

    SpringBoot对静态资源的映射规则 @ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFi ...

  5. linux初始化

    [Linux 系统启动过程] Linux的启动其实和windows的启动过程很类似,不过windows我们是无法看到启动信息的,而linux启动时我们会看到许多启动信息,例如某个服务是否启动. Lin ...

  6. [CI]CodeIgniter特性 & 结构

    ------------------------------------------------------------------------------------------------- 市场 ...

  7. [PC]PHPCMS v9.5.6整合UEditer1.4.2

    ----------------------------------------------------------------------------------------------- 首先去U ...

  8. 用Delphi制作DLL

    一.开使你的第一个DLL专案  1.File->Close all->File->New﹝DLL﹞代码: //自动产生Code如下  library Project2;  //这有段 ...

  9. java容器的理解(collection)

    容器类(Conllection)对于一个开发者来说是最强大的工具之一,可以大幅提高编程能力.容器是一个将多个元素组合到一个单元的对象,是代表一组对象的对象,容器中的对象成为它的元素. 容器适用于处理各 ...

  10. hdu3189-Just Do It-(埃氏筛+唯一分解定理)

    Just Do It Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...