1+ package  com .leetcode .trees ;
2+ 
3+ 
4+ import  java .util .LinkedList ;
5+ import  java .util .Queue ;
6+ 
7+ /** 
8+  * Level: Hard 
9+  * Link: https://leetcode.com/problems/serialize-and-deserialize-binary-tree/ 
10+  * Description: 
11+  * Serialization is the process of converting a data structure or object into a sequence of bits so that it can be 
12+  * stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in 
13+  * the same or another computer environment. 
14+  * 
15+  * Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your 
16+  * serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized 
17+  * to a string and this string can be deserialized to the original tree structure. 
18+  * 
19+  * Example: 
20+  * 
21+  * You may serialize the following tree: 
22+  * 
23+  *     1 
24+  *    / \ 
25+  *   2   3 
26+  *      / \ 
27+  *     4   5 
28+  * 
29+  * as "[1,2,3,null,null,4,5]" 
30+  * 
31+  * Clarification: The above format is the same as how LeetCode serializes a binary tree. You do not necessarily need 
32+  * to follow this format, so please be creative and come up with different approaches yourself. 
33+  * 
34+  * Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms 
35+  * should be stateless. 
36+  * 
37+  * @author rampatra 
38+  * @since 2019-08-17 
39+  */ 
40+ public  class  SerializeDeserializeBinaryTree  {
41+ 
42+     /** 
43+      * Runtime: <a href="https://leetcode.com/submissions/detail/252443769/">31 ms</a>. 
44+      * 
45+      * @param root 
46+      * @return 
47+      */ 
48+     public  static  String  serialize (TreeNode  root ) {
49+         if  (root  == null ) {
50+             return  "[]" ;
51+         }
52+ 
53+         StringBuilder  sb  = new  StringBuilder ();
54+         sb .append ("[" );
55+ 
56+         Queue <TreeNode > queue  = new  LinkedList <>();
57+         queue .add (root );
58+ 
59+         while  (!queue .isEmpty ()) {
60+             TreeNode  node  = queue .poll ();
61+ 
62+             if  (sb .length () > 1 ) {
63+                 sb .append (", " );
64+             }
65+             if  (node  == null ) {
66+                 sb .append ("null" );
67+                 continue ;
68+             }
69+ 
70+             sb .append (node .val );
71+ 
72+             queue .add (node .left );
73+             queue .add (node .right );
74+         }
75+ 
76+         sb .append ("]" );
77+         return  removeExtraNulls (sb .toString ());
78+     }
79+ 
80+     private  static  String  removeExtraNulls (String  data ) {
81+         int  i  = data .length () - 1 ;
82+         while  (!(data .charAt (i ) >= 48  && data .charAt (i ) <= 57 )) {
83+             i --;
84+         }
85+         return  data .substring (0 , i  + 1 ) + "]" ;
86+     }
87+ 
88+     /** 
89+      * 
90+      * @param data 
91+      * @return 
92+      */ 
93+     public  static  TreeNode  deserialize (String  data ) {
94+         data  = data .substring (1 , data .length () - 1 );
95+ 
96+         if  (data .length () == 0 ) {
97+             return  null ;
98+         }
99+ 
100+         String [] values  = data .split (", " );
101+ 
102+         TreeNode  root  = new  TreeNode (Integer .parseInt (values [0 ]));
103+ 
104+         Queue <TreeNode > queue  = new  LinkedList <>();
105+         queue .add (root );
106+ 
107+         for  (int  i  = 0 ; i  < values .length  && !queue .isEmpty (); i  += 2 ) {
108+             TreeNode  currNode  = queue .poll ();
109+ 
110+             if  (i  + 1  < values .length  && !values [i  + 1 ].equals ("null" )) {
111+                 TreeNode  leftNode  = new  TreeNode (Integer .parseInt (values [i  + 1 ]));
112+                 currNode .left  = leftNode ;
113+                 queue .add (leftNode );
114+             }
115+ 
116+             if  (i  + 2  < values .length  && !values [i  + 2 ].equals ("null" )) {
117+                 TreeNode  rightNode  = new  TreeNode (Integer .parseInt (values [i  + 2 ]));
118+                 currNode .right  = rightNode ;
119+                 queue .add (rightNode );
120+             }
121+         }
122+ 
123+         return  root ;
124+     }
125+ 
126+     public  static  void  main (String [] args ) {
127+         // TODO Convert the print statements to asserts 
128+ 
129+         System .out .println (serialize (new  TreeNode (1 )));
130+ 
131+         /* 
132+             Binary Tree 
133+ 
134+                 1 
135+                / \ 
136+               2   3 
137+              / \ 
138+             4   5 
139+          */ 
140+         TreeNode  tree  = new  TreeNode (1 );
141+         tree .left  = new  TreeNode (2 );
142+         tree .right  = new  TreeNode (3 );
143+         tree .left .left  = new  TreeNode (4 );
144+         tree .left .right  = new  TreeNode (5 );
145+ 
146+         System .out .println (serialize (tree ));
147+ 
148+         System .out .println (serialize (deserialize (serialize (tree ))));
149+ 
150+         System .out .println (serialize (deserialize (serialize (null ))));
151+ 
152+         TreeNode  tree2  = new  TreeNode (1 );
153+         tree2 .right  = new  TreeNode (2 );
154+         tree2 .right .right  = new  TreeNode (3 );
155+         tree2 .right .right .right  = new  TreeNode (4 );
156+         tree2 .right .right .right .right  = new  TreeNode (5 );
157+         tree2 .right .right .right .right .right  = new  TreeNode (6 );
158+         tree2 .right .right .right .right .right .right  = new  TreeNode (7 );
159+         tree2 .right .right .right .right .right .right .right  = new  TreeNode (8 );
160+ 
161+         System .out .println (serialize (tree2 ));
162+         System .out .println (serialize (deserialize (serialize (tree2 ))));
163+ 
164+         System .out .println ("---" );
165+ 
166+         System .out .println (serialize (deserialize ("[1, 2]" )));
167+         System .out .println (serialize (deserialize ("[1, 2, 3]" )));
168+         System .out .println (serialize (deserialize ("[3, 2, 4, 1]" )));
169+         System .out .println (serialize (deserialize ("[3, 2, 4, 1, 5, 6]" )));
170+         System .out .println (serialize (deserialize ("[1, 2, 3, null, null, 4, 5]" )));
171+         System .out .println (serialize (deserialize ("[5, 2, 3, null, null, 2, 4, 3, 1]" )));
172+ 
173+         System .out .println (serialize (deserialize ("[1, null, 2, null, 3, null, 4, null, 5]" )));
174+         System .out .println (serialize (deserialize ("[1, null, 2, null, 3, null, 4, null, 5, null, 6]" )));
175+         System .out .println (serialize (deserialize ("[1, null, 2, null, 3, null, 4, null, 5, null, 6, null, 7]" )));
176+         System .out .println (serialize (deserialize ("[1, null, 2, null, 3, null, 4, null, 5, null, 6, null, 7, null, 8]" )));
177+         System .out .println (serialize (deserialize ("[1, null, 2, null, 3, null, 4, null, 5, null, 6, null, 7, null, 8, null, 9]" )));
178+     }
179+ }
0 commit comments