zoukankan      html  css  js  c++  java
  • Leetcode: Serialize and Deserialize Binary Tree

    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 tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.
    
    For example, you may serialize the following tree
    
        1
       / 
      2   3
         / 
        4   5
    as "[1,2,3,null,null,4,5]", just the same as how LeetCode OJ serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
    Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.

    Referring this post: https://leetcode.com/problems/serialize-and-deserialize-binary-tree/discuss/74253/Easy-to-understand-Java-Solution

    But instead using deque, use queue instead.

    queue inherits addAll() function from java.util.Collection interface.

    queue itself has poll() function to remove the head of the queue.

    We can actually use queue, linkedlist, deque here as the variable type, but the actually implementation should allways be linkedlist.

    if we define the type to be List<String>, it won't work because List doesn't have poll() function

    Also notice that split() will not have trailing empty strings in the result array.

    Some static functions:

    Arrays.asList(int[] arr);    // array to List

    String.valueOf(int i);   // int to String

    Integer.valueOf(String s);  // String to int

     1 public class Codec {
     2     final String spliter = ",";
     3     final String NN = "X";
     4 
     5     // Encodes a tree to a single string.
     6     public String serialize(TreeNode root) {
     7         StringBuilder sb = new StringBuilder();
     8         buildString(root, sb);
     9         return sb.toString();
    10     }
    11     
    12     public void buildString(TreeNode node, StringBuilder sb) {
    13         if (node == null) {
    14             sb.append(NN).append(spliter);
    15             return;
    16         }
    17         sb.append(node.val).append(spliter);
    18         buildString(node.left, sb);
    19         buildString(node.right, sb);
    20     }
    21 
    22     // Decodes your encoded data to tree.
    23     public TreeNode deserialize(String data) {
    24         Queue<String> queue = new LinkedList<> ();
    25         queue.addAll(Arrays.asList(data.split(spliter)));
    26         return constructTree(queue);
    27     }
    28     
    29     public TreeNode constructTree(Queue<String> queue) {
    30         String val = queue.poll();
    31         if (val.equals(NN)) return null;
    32         TreeNode node = new TreeNode(Integer.valueOf(val));
    33         node.left = constructTree(queue);
    34         node.right = constructTree(queue);
    35         return node;
    36     }
    37 }

    Follow Up:

    如果已经知道这个Binary Tree的size为T, 怎么估计输出链表size?

    答案是 2*T+1,    (假设tree perfect and complete, leaf node层有大概一半的node数,比之前所有层的node数之和还要多一,那么,最后一层node每个再派生两个“#”, 个数就会有T+1个,所以总共有2*T+1个)

  • 相关阅读:
    对于程序员来说,如何才能快速转行赶上大数据这辆高铁呢?
    大数据学习计划
    大数据学习详细路线
    大数据到底要怎么学习?
    大数据处理技术怎么学习呢?
    从零开始大数据学习路线
    大数据从入门到精通
    大数据学习路线
    8.【Spring Cloud Alibaba】配置管理-Nacos
    7.【Spring Cloud Alibaba】微服务的用户认证与授权
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/5084538.html
Copyright © 2011-2022 走看看