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.
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public int val; 5 * public TreeNode left; 6 * public TreeNode right; 7 * public TreeNode(int x) { val = x; } 8 * } 9 */ 10 public class Codec { 11 12 private void DFS(StringBuilder sb, TreeNode node) 13 { 14 if (sb.Length > 0) sb.Append(","); 15 16 if (node == null) 17 { 18 sb.Append("null"); 19 return; 20 } 21 22 sb.Append(node.val); 23 DFS(sb, node.left); 24 DFS(sb, node.right); 25 } 26 27 // Encodes a tree to a single string. 28 public string serialize(TreeNode root) { 29 var sb = new StringBuilder(); 30 DFS(sb, root); 31 return sb.ToString(); 32 } 33 34 // Decodes your encoded data to tree. 35 public TreeNode deserialize(string data) { 36 var nodes = data.Split(','); 37 38 if (nodes.Length == 0) return null; 39 40 return ConstructTree(nodes.ToList()); 41 } 42 43 private TreeNode ConstructTree(List<string> nodes) 44 { 45 if (nodes.Count == 0) return null; 46 47 var n = nodes[0]; 48 nodes.RemoveAt(0); 49 50 if (n == "null") return null; 51 52 var root = new TreeNode(Int32.Parse(n)); 53 root.left = ConstructTree(nodes); 54 root.right = ConstructTree(nodes); 55 56 return root; 57 } 58 } 59 60 public class CodecBFS { 61 62 // Encodes a tree to a single string. 63 public string serialize(TreeNode root) { 64 var sb = new StringBuilder(); 65 var q = new Queue<TreeNode>(); 66 67 q.Enqueue(root); 68 69 while (q.Count > 0) 70 { 71 var n = q.Dequeue(); 72 73 if (sb.Length > 0) sb.Append(","); 74 75 if (n == null) 76 { 77 sb.Append("null"); 78 } 79 else 80 { 81 sb.Append(n.val); 82 q.Enqueue(n.left); 83 q.Enqueue(n.right); 84 } 85 } 86 87 return sb.ToString(); 88 } 89 90 // Decodes your encoded data to tree. 91 public TreeNode deserialize(string data) { 92 var nodes = data.Split(','); 93 94 if (nodes.Length == 0) return null; 95 96 var q = new Queue<TreeNode>(); 97 TreeNode root = null; 98 99 for (int i = 0; i < nodes.Length; i++) 100 { 101 var n = nodes[i]; 102 if (q.Count == 0) 103 { 104 if (n == "null") 105 { 106 break; 107 } 108 else 109 { 110 root = new TreeNode(Int32.Parse(n)); 111 q.Enqueue(root); 112 } 113 } 114 else 115 { 116 var p = q.Dequeue(); 117 if (n != "null") 118 { 119 var left = new TreeNode(Int32.Parse(n)); 120 p.left = left; 121 q.Enqueue(left); 122 } 123 124 if (i + 1 < nodes.Length) 125 { 126 n = nodes[++i]; 127 if (n != "null") 128 { 129 var right = new TreeNode(Int32.Parse(n)); 130 p.right = right; 131 q.Enqueue(right); 132 } 133 } 134 } 135 } 136 137 return root; 138 } 139 } 140 141 // Your Codec object will be instantiated and called as such: 142 // Codec codec = new Codec(); 143 // codec.deserialize(codec.serialize(root));