297. Serialize and Deserialize Binary Tree

297. Serialize and Deserialize Binary Tree


public class Codec {
  
    private static final String spliter = ",";
    private static final String NULL = "X";
  

    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
      StringBuilder sb = new StringBuilder();
      helper(root, sb);
      return sb.toString();
    }
  
    private void helper(TreeNode root, StringBuilder sb){
      if(root == null){
        sb.append(NULL).append(spliter);
      }else{
        sb.append(root.val).append(spliter);
        helper(root.left, sb);
        helper(root.right, sb);
      }
    }
  

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
      Queue<String> queue = new LinkedList<>();         // addAll(). Arrays.asList()
      queue.addAll(Arrays.asList(data.split(spliter))); // put the string to a list and add it to the queue 
      return helper(queue);
    }
    
    private TreeNode helper(Queue<String> queue){
      String val = queue.poll(); // for npe, use poll or remove? 
      if(val.equals(NULL)){ // don‘t use ==, use .equals()
        return null;
      }
      TreeNode root = new TreeNode(Integer.valueOf(val)); // TreeNode(Integer.valueOf(val)); convert string to integer 
      root.left = helper(queue);
      root.right = helper(queue);
      return root;
        
    }
}

// The remove and poll methods differ in their behavior only when the queue is empty. Under these circumstances, remove throws NoSuchElementException , while poll returns null 

//Main difference between .equals() method and == operator is that one is method and other is operator.
//We can use == operators for reference comparison (address comparison) and .equals() method for content comparison. In simple words, == checks if both objects point to the same memory location whereas .equals() evaluates to the comparison of values in the objects.
//If a class does not override the equals method, then by default it uses equals(Object o) method of the closest parent class that has overridden this method. See this for detail


// https://www.geeksforgeeks.org/override-equalsobject-hashcode-method/
=======================================
  
preorder 
         1
     2        3
  4    5    6    7
      
1, 2, 4, X, X, 5, X, X, 3, 6, X, X, 7, X,X

======================================

          1
     2         3
  4     5    6     7
       9      10 
  
1, 2, 4, X, X, 5, 9, X, X, X, 3, 6, X, 10, X, X, 7, X, X


=======================================

 

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.

Example: 

You may serialize the following tree:

    1
   / \
  2   3
     / \
    4   5

as "[1,2,3,null,null,4,5]"

Clarification: The above format is the same as how LeetCode 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.

posted on 2018-08-09 18:33  猪猪&#128055;  阅读(118)  评论(0)    收藏  举报

导航