public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { this.val = val; this.left = left; this.right = right; } } public class Codec { public string serialize(TreeNode root) { if (root == null) return "#"; return root.val + "," + serialize(root.left) + "," + serialize(root.right); } } // Tree: // 1 // / \ // 2 3 // / \ // 4 5 TreeNode root = new TreeNode(1, new TreeNode(2), new TreeNode(3, new TreeNode(4), new TreeNode(5)) ); Codec codec = new Codec(); string result = codec.serialize(root); System.Console.WriteLine(result);
The serialization uses preorder traversal: root, left, right.
Null children are represented by '#'.
So the tree serializes as: 1 (root), 2 (left child), # (2's left null), # (2's right null), 3 (right child), 4 (3's left), # (4's left null), # (4's right null), 5 (3's right), # (5's left null), # (5's right null).
public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { this.val = val; this.left = left; this.right = right; } } public class Codec { private int index = 0; private string[] nodes; public TreeNode deserialize(string data) { nodes = data.Split(','); index = 0; return helper(); } private TreeNode helper() { if (index >= nodes.Length || nodes[index] == "#") { index++; return null; } TreeNode root = new TreeNode(int.Parse(nodes[index++])); root.left = helper(); root.right = helper(); return root; } } Codec codec = new Codec(); TreeNode root = codec.deserialize("1,2,#,#,3,4,#,#,5,#,#"); System.Console.WriteLine(root.val);
The deserialize method reads the first value as the root node's value.
Since the first value is '1', the root node's value is 1.
public class Codec { private int index = 0; private string[] nodes; public TreeNode deserialize(string data) { nodes = data.Split(','); return helper(); } private TreeNode helper() { if (index >= nodes.Length || nodes[index] == "#") { index++; return null; } TreeNode root = new TreeNode(int.Parse(nodes[index++])); root.left = helper(); root.right = helper(); return root; } }
The index variable is not reset to 0 before starting the recursive helper calls.
If deserialize is called multiple times on the same Codec instance, index may be beyond the array length, causing an index out of range error.
public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { this.val = val; this.left = left; this.right = right; } } public class Codec { public string serialize(TreeNode root) { if (root == null) return ""; var queue = new System.Collections.Generic.Queue<TreeNode>(); queue.Enqueue(root); var result = new System.Text.StringBuilder(); while (queue.Count > 0) { var node = queue.Dequeue(); if (node == null) { result.Append("#,"); continue; } result.Append(node.val + ","); queue.Enqueue(node.left); queue.Enqueue(node.right); } return result.ToString().TrimEnd(','); } } // Tree: // 1 // / \ // 2 3 // / \ // 4 5 TreeNode root = new TreeNode(1, new TreeNode(2), new TreeNode(3, new TreeNode(4), new TreeNode(5)) ); Codec codec = new Codec(); string output = codec.serialize(root); System.Console.WriteLine(output);
The BFS traversal visits nodes in this order: 1, 2, 3, null, null, 4, 5, null, null, null, null.
Null children of 2 are represented by '#', similarly for 4 and 5.
In a binary tree with N nodes, there are N+1 null children in total.
Preorder serialization records each node and each null child.
So total elements = N (nodes) + (N+1) (nulls) = 2N + 1.
