|
| 1 | +import java.util.*; |
| 2 | + |
| 3 | +// Define the TreeNode class to represent nodes in the binary tree. |
| 4 | +class TreeNode { |
| 5 | + int val; // Value stored in the node |
| 6 | + TreeNode left; // Pointer to the left child |
| 7 | + TreeNode right; // Pointer to the right child |
| 8 | + |
| 9 | + // Constructor to create a new node with a given value |
| 10 | + TreeNode(int val) { |
| 11 | + this.val = val; |
| 12 | + this.left = null; // Initially, the left child is null |
| 13 | + this.right = null; // Initially, the right child is null |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +public class BinaryTreeSerializer { |
| 18 | + |
| 19 | + // The 'serialize' method converts a binary tree into a string. |
| 20 | + public String serialize(TreeNode root) { |
| 21 | + // If the tree is empty (root is null), return an empty string. |
| 22 | + if (root == null) return ""; |
| 23 | + |
| 24 | + // StringBuilder is used to build the final serialized string efficiently. |
| 25 | + StringBuilder sb = new StringBuilder(); |
| 26 | + |
| 27 | + // We will use a queue to perform level-order traversal (breadth-first search). |
| 28 | + Queue<TreeNode> queue = new LinkedList<>(); |
| 29 | + |
| 30 | + // Start by adding the root node to the queue. |
| 31 | + queue.add(root); |
| 32 | + |
| 33 | + // Process each node in the queue, and append its value (or "null" for empty nodes) to the result. |
| 34 | + while (!queue.isEmpty()) { |
| 35 | + // Remove the front node from the queue. |
| 36 | + TreeNode node = queue.poll(); |
| 37 | + |
| 38 | + // If the node is not null, process its value and add its children to the queue. |
| 39 | + if (node != null) { |
| 40 | + // Append the node's value to the string, followed by a comma to separate values. |
| 41 | + sb.append(node.val).append(","); |
| 42 | + |
| 43 | + // Add the left and right children to the queue (even if they are null). |
| 44 | + queue.add(node.left); |
| 45 | + queue.add(node.right); |
| 46 | + } else { |
| 47 | + // If the node is null, append "null" to the string to represent the absence of a node. |
| 48 | + sb.append("null,"); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // Remove the last comma from the end of the string to make it clean. |
| 53 | + sb.setLength(sb.length() - 1); |
| 54 | + |
| 55 | + // Return the final serialized string. |
| 56 | + return sb.toString(); |
| 57 | + } |
| 58 | + |
| 59 | + // The 'deserialize' method converts a string back into a binary tree. |
| 60 | + public TreeNode deserialize(String data) { |
| 61 | + // If the input string is empty, return null (i.e., the tree is empty). |
| 62 | + if (data == null || data.isEmpty()) return null; |
| 63 | + |
| 64 | + // Split the string by commas to get the serialized node values. |
| 65 | + String[] nodes = data.split(","); |
| 66 | + |
| 67 | + // The first value in the string is the root node. |
| 68 | + TreeNode root = new TreeNode(Integer.parseInt(nodes[0])); |
| 69 | + |
| 70 | + // Use a queue to manage the nodes during reconstruction of the tree. |
| 71 | + Queue<TreeNode> queue = new LinkedList<>(); |
| 72 | + queue.add(root); |
| 73 | + |
| 74 | + // Use an index to track the current position in the node array (starting from 1, since 0 is root). |
| 75 | + int index = 1; |
| 76 | + |
| 77 | + // Process each node in the queue to reconstruct the tree. |
| 78 | + while (!queue.isEmpty()) { |
| 79 | + // Get the current node from the queue. |
| 80 | + TreeNode node = queue.poll(); |
| 81 | + |
| 82 | + // Rebuild the left child. |
| 83 | + if (!nodes[index].equals("null")) { |
| 84 | + // If the value is not "null", create a new node and add it to the left. |
| 85 | + node.left = new TreeNode(Integer.parseInt(nodes[index])); |
| 86 | + // Add the left child to the queue for further processing. |
| 87 | + queue.add(node.left); |
| 88 | + } |
| 89 | + index++; // Move to the next value in the array. |
| 90 | + |
| 91 | + // Rebuild the right child. |
| 92 | + if (!nodes[index].equals("null")) { |
| 93 | + // If the value is not "null", create a new node and add it to the right. |
| 94 | + node.right = new TreeNode(Integer.parseInt(nodes[index])); |
| 95 | + // Add the right child to the queue for further processing. |
| 96 | + queue.add(node.right); |
| 97 | + } |
| 98 | + index++; // Move to the next value in the array. |
| 99 | + } |
| 100 | + |
| 101 | + // Return the root of the reconstructed tree. |
| 102 | + return root; |
| 103 | + } |
| 104 | + |
| 105 | + public static void main(String[] args) { |
| 106 | + // Example of creating a binary tree manually. |
| 107 | + // 1 |
| 108 | + // / \ |
| 109 | + // 2 3 |
| 110 | + // / \ |
| 111 | + // 4 5 |
| 112 | + |
| 113 | + TreeNode root = new TreeNode(1); // Create the root node with value 1 |
| 114 | + root.left = new TreeNode(2); // Create the left child (value 2) |
| 115 | + root.right = new TreeNode(3); // Create the right child (value 3) |
| 116 | + root.right.left = new TreeNode(4); // Create the left child of node 3 (value 4) |
| 117 | + root.right.right = new TreeNode(5); // Create the right child of node 3 (value 5) |
| 118 | + |
| 119 | + // Instantiate the BinaryTreeSerializer class |
| 120 | + BinaryTreeSerializer serializer = new BinaryTreeSerializer(); |
| 121 | + |
| 122 | + // Serialize the tree to a string format |
| 123 | + String serializedTree = serializer.serialize(root); |
| 124 | + System.out.println("Serialized Tree: " + serializedTree); |
| 125 | + // Expected output: "1,2,3,null,null,4,5,null,null,null,null" |
| 126 | + |
| 127 | + // Deserialize the string back to a binary tree |
| 128 | + TreeNode deserializedRoot = serializer.deserialize(serializedTree); |
| 129 | + System.out.println("Tree deserialized successfully. Root value: " + deserializedRoot.val); |
| 130 | + // Expected output: Root value should be 1 |
| 131 | + } |
| 132 | +} |
| 133 | + |
0 commit comments