Skip to content

Commit 95dda15

Browse files
committed
Serialize and Deserialize the Binary Tree added
1 parent 48fa2cd commit 95dda15

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/test/java/com/thealgorithms/datastructures/trees/SerializeaBinaryTree.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import java.util.*;
1+
import java.util.LinkedList; // Replace with specific imports
2+
import java.util.Queue;
23

34
// Define the TreeNode class to represent nodes in the binary tree.
45
class TreeNode {
@@ -19,7 +20,9 @@ public class SerializeaBinaryTree {
1920
// The 'serialize' method converts a binary tree into a string.
2021
public String serialize(TreeNode root) {
2122
// If the tree is empty (root is null), return an empty string.
22-
if (root == null) return "";
23+
if (root == null) {
24+
return "";
25+
}
2326

2427
// StringBuilder is used to build the final serialized string efficiently.
2528
StringBuilder sb = new StringBuilder();
@@ -59,7 +62,9 @@ public String serialize(TreeNode root) {
5962
// The 'deserialize' method converts a string back into a binary tree.
6063
public TreeNode deserialize(String data) {
6164
// If the input string is empty, return null (i.e., the tree is empty).
62-
if (data == null || data.isEmpty()) return null;
65+
if (data == null || data.isEmpty()) {
66+
return null;
67+
}
6368

6469
// Split the string by commas to get the serialized node values.
6570
String[] nodes = data.split(",");

0 commit comments

Comments
 (0)