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. js实现上传前删除指定图片

    "上传之前"移除选错图片代码: 此处效果为:点击需要删除的图片,确认删除就可以了.

  2. JAVA8-待续

    1. 函数式编程,因为在并发和时间驱动编程中的优势,函数式编程又逐渐流行起来 以前是实现一个比较器需要实现Comparator接口,并重写compare方法,以下为两种实现方法(类似还有线程,事件等) ...

  3. php版本升级导致openssl无法使用

    也就是call to undefined function openssl错误: 把extension前面的注释去掉,甚至把“libeay32.dll和ssleay32.dll文件复制并替换到apac ...

  4. 装饰者模式——Head First

    一.定义 装饰者模式(Decorator Pattern)动态地将责任附加到对象上.若要扩展功能,装饰者提供了比继承更有弹性的替代方案. 二.类图 三.星巴兹饮料 //Component public ...

  5. C++中类的多继承

    在写这一主题的文章之前,在网上找到一篇很非常好的文章C++之继承与多态.就没有必要做重复造轮子的事件了,那就从这篇文章开始吧! 在c++中一个类可以从多个基类中派生(即可以有多个父类),这就是多继承. ...

  6. 多线程(Java)

    Thread 类 和 Runnable 接口 在Java中实现多线程有两种方法 继承 Thread 类 优点:通过覆盖Thread 类的方法,可以改变线程的行为. 实现 Runnable 接口 优点: ...

  7. 虚拟机安装centos6.6全步骤

    1.首先要下载一个centos的iso镜像,我是用虚拟机VMware来安装的,用VMware最好创建一个空白硬盘. 2.创建完毕再设置里面挂载iso的centos系统文件. 3.进入到这个页面: 说明 ...

  8. requirejs源码分析

  9. 解决ngui挡住粒子的问题

    using UnityEngine; using System.Collections; [ExecuteInEditMode] public class ControlParticle : Mono ...

  10. 百度开放平台连接MySQL数据库

    在百度开放平台创建了MySQL数据库后只知道数据库名称,可以通过下面的方法进行连接: public function connect(){ $_server = getenv('HTTP_BAE_EN ...