zoukankan      html  css  js  c++  java
  • [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.

    题目

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

    思路

    这个题最大不同是 “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

    代码

     1 public class Codec {
     2     // Encodes a tree to a single string.
     3     public String serialize(TreeNode root) {
     4         StringBuilder sb = new StringBuilder();
     5         buildString(root, sb);
     6         return sb.toString();
     7     }
     8     
     9     private void buildString(TreeNode root, StringBuilder sb) {
    10         if (root == null) return; 
    11         /*Q: 为何不将 root == null 加到stringbuilder里?
    12          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
    13          separators, which makes it the most compact possible.
    14          Q: 为何选择pre-order
    15          preorder traversal 的时候, 对于当前节点,所有的左节点比他小,右节点比他大
    16         */
    17         sb.append(root.val).append(",");
    18         buildString(root.left, sb);
    19         buildString(root.right, sb);
    20     }
    21 
    22     // Decodes your encoded data to tree.
    23     public TreeNode deserialize(String data) {
    24         if (data.length() == 0) return null;
    25         // 
    26         int[] idx = new int[]{0};
    27         idx[0] = 0;
    28         return buildTree(data.split(","), idx, Integer.MIN_VALUE, Integer.MAX_VALUE);
    29     }
    30     
    31     private TreeNode buildTree(String[] nodes, int[] idx, int min, int max) {
    32         if (idx[0] == nodes.length) return null;
    33         int val = Integer.parseInt(nodes[idx[0]]);
    34         if (val < min || val > max) return null; // Go back if we are over the boundary
    35         TreeNode cur = new TreeNode(val);
    36         idx[0]++; // update current position
    37         /*keep with encode pre-order*/
    38         cur.left = buildTree(nodes, idx, min, val);
    39         cur.right = buildTree(nodes, idx, val, max);
    40         return cur;
    41     }
    42 }
  • 相关阅读:
    【总结整理】互联网名词
    【总结整理】数据分析对产品有什么用
    【总结整理】互联网产品的功能设计怎么做
    【总结整理】产品的信息架构
    【总结整理】用完即走
    【总结整理】如何系统地规划出具备上乘用户体验的网站--摘自《人人都是产品经理》
    【总结整理】如何与用户交流---摘自《人人都是产品经理》
    【总结整理】原创概念原创idea---摘自《结网》
    【总结整理】模仿的本质---摘自《结网》
    【转】深入理解java的String
  • 原文地址:https://www.cnblogs.com/liuliu5151/p/9823995.html
Copyright © 2011-2022 走看看