Advertisement
1988coder

449. Serialize and Deserialize BST

Nov 28th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.08 KB | None | 0 0
  1. /**
  2.  * Definition for a binary tree node.
  3.  * public class TreeNode {
  4.  *     int val;
  5.  *     TreeNode left;
  6.  *     TreeNode right;
  7.  *     TreeNode(int x) { val = x; }
  8.  * }
  9.  */
  10. /*
  11. Serialize function
  12. Time Complexity: O(M * K) = O(M * 11) = O(M)
  13. Space Complexity: O(M * K) = O(M * 11) = O(M)
  14. M = Number of Nodes
  15. K = Average Length of each node. (Bounded by 11)
  16.  
  17. Deserialize function
  18. Time Complexity: O(N + M + M*K) = O(N + M*K) = O(N+N) = O(N)
  19. Space Complexity: O(M * K) = O(N)
  20. N = Length of the input string
  21. M = Number of Nodes
  22. K = Average Length of each node. (Bounded by 11)
  23. */
  24. public class Codec {
  25.  
  26.     // Encodes a tree to a single string.
  27.     public String serialize(TreeNode root) {
  28.         if (root == null) {
  29.             return "";
  30.         }
  31.         List<String> list = new ArrayList();
  32.         serializeHelper(list, root);
  33.         return String.join(",", list);
  34.     }
  35.    
  36.     private void serializeHelper(List<String> list, TreeNode node) {
  37.         if (node == null) {
  38.             return;
  39.         }
  40.         list.add(String.valueOf(node.val));
  41.         serializeHelper(list, node.left);
  42.         serializeHelper(list, node.right);
  43.     }
  44.  
  45.     // Decodes your encoded data to tree.
  46.     public TreeNode deserialize(String data) {
  47.         if (data == null || data.length() == 0) {
  48.             return null;
  49.         }
  50.         // Queue<String> queue = new LinkedList(Arrays.asList(data.split(",")));
  51.         // return deserializeHelper(queue, Integer.MIN_VALUE, Integer.MAX_VALUE);
  52.         List<String> list = Arrays.asList(data.split(","));
  53.         return deserializeHelper(list, new int[1], Integer.MIN_VALUE, Integer.MAX_VALUE);
  54.     }
  55.    
  56.     // This helper function uses queue to construct the tree.
  57.     private TreeNode deserializeHelper(Queue<String> queue, int min, int max) {
  58.         if (queue == null || queue.isEmpty()) {
  59.             return null;
  60.         }
  61.         int valToBeInserted = Integer.parseInt(queue.peek());
  62.         if (valToBeInserted < min || valToBeInserted > max) {
  63.             return null;
  64.         }
  65.         queue.poll();
  66.         TreeNode node = new TreeNode(valToBeInserted);
  67.         node.left = deserializeHelper(queue, min, valToBeInserted);
  68.         node.right = deserializeHelper(queue, valToBeInserted, max);
  69.         return node;
  70.     }
  71.    
  72.     // This helper function uses list and index to construct the tree.
  73.     private TreeNode deserializeHelper(List<String> list, int[] index, int min, int max) {
  74.         if (index[0] == list.size()) {
  75.             return null;
  76.         }
  77.         int valToBeInserted = Integer.parseInt(list.get(index[0]));
  78.         if (valToBeInserted < min || valToBeInserted > max) {
  79.             return null;
  80.         }
  81.         index[0]++;
  82.         TreeNode node = new TreeNode(valToBeInserted);
  83.         node.left = deserializeHelper(list, index, min, valToBeInserted);
  84.         node.right = deserializeHelper(list, index, valToBeInserted, max);
  85.         return node;
  86.     }
  87. }
  88.  
  89. // Your Codec object will be instantiated and called as such:
  90. // Codec codec = new Codec();
  91. // codec.deserialize(codec.serialize(root));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement