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. HBuilder开发APP自动登录时跳过"登录页面"

    刚接触开发公司APP项目,用HBuilder开发工具. manifest.json中的入口页面就是"登录页面",现在获取到自动登录状态是true,但是真机联调时"登录页面 ...

  2. Servlet基本_Httpリクエスト、レスポンス

    1.リクエスト リクエストは.リクエストライン.メッセージヘッダ.改行.メッセージボディで組まれる. 主なリクエストヘッダは. Accept クライアントが利用可能なデータメディアタイプを指定. Ac ...

  3. openwrt手工配置pptpd

    官方wiki:http://wiki.openwrt.org/doc/howto/vpn.server.pptpd#prerequisites 20190220更新:PPTP VPN协议已经被 IOS ...

  4. tomcat 简单安装配置说明

    1.下载安装文件[apache-tomcat-6.0.41.tar.gz]到指定目录[/usr/local]2.解压文件[tar -zxvf apache-tomcat-6.0.41.tar.gz], ...

  5. Git设置/取消代理

    设置代理 git config --global http.proxy http://proxy.com:1234 git config --global https.proxy http://pro ...

  6. powerdesiner技巧

    1.name和code同步问题和name成comments http://blog.csdn.net/huang_xw/article/details/5722981 2.连接数据库

  7. C++ Custom Control控件 向父窗体发送对应的消息

    向父窗体发送消息 ,这里只讲发送 WM_NOTIFY  消息, 其它消息是相同的 在 控件中的某个函数中 设置发送消息的程序 首先定义一个WM_NOTIFY消息的专用结构. NMHDR nm; nm. ...

  8. java.lang.StringIndexOutOfBoundsException: String index out of range: 0

    hibernet 报错 java.lang.StringIndexOutOfBoundsException: String index out of range: 0 处理方法  数据表字段为char ...

  9. Java NIO Tutorial

    Java NIO Tutorial     Jakob JenkovLast update: 2014-06-25

  10. 2018面向对象程序设计(Java)第4周学习指导及要求

    2018面向对象程序设计(Java) 第4周学习指导及要求(2017.9.19-2017.9. 26)   学习目标 掌握类与对象的基础概念,理解类与对象的关系: 掌握对象与对象变量的关系: 掌握预定 ...