Skip to content

Commit bebf372

Browse files
Solution Leetcode and Explaination Every single line
1 parent 7f8e231 commit bebf372

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
public class Codec {
2+
3+
// Serialize → Tree to String
4+
public String serialize(TreeNode root) {
5+
if (root == null) return "null";
6+
7+
StringBuilder sb = new StringBuilder();
8+
Queue<TreeNode> q = new LinkedList<>();
9+
q.offer(root);
10+
11+
while (!q.isEmpty()) {
12+
TreeNode node = q.poll();
13+
if (node == null) {
14+
sb.append("null,");
15+
continue;
16+
}
17+
sb.append(node.val).append(",");
18+
q.offer(node.left);
19+
q.offer(node.right);
20+
}
21+
22+
return sb.toString();
23+
}
24+
25+
// Deserialize → String to Tree
26+
public TreeNode deserialize(String data) {
27+
if (data.equals("null")) return null;
28+
29+
String[] vals = data.split(",");
30+
TreeNode root = new TreeNode(Integer.parseInt(vals[0]));
31+
Queue<TreeNode> q = new LinkedList<>();
32+
q.offer(root);
33+
int i = 1;
34+
35+
while (!q.isEmpty()) {
36+
TreeNode curr = q.poll();
37+
if (!vals[i].equals("null")) {
38+
curr.left = new TreeNode(Integer.parseInt(vals[i]));
39+
q.offer(curr.left);
40+
}
41+
i++;
42+
if (!vals[i].equals("null")) {
43+
curr.right = new TreeNode(Integer.parseInt(vals[i]));
44+
q.offer(curr.right);
45+
}
46+
i++;
47+
}
48+
49+
return root;
50+
}
51+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
LeetCode 297 – Serialize and Deserialize Binary Tree
2+
3+
public class Codec {
4+
5+
// Serialize → Tree to String
6+
public String serialize(TreeNode root) {
7+
if (root == null) return "null";
8+
9+
StringBuilder sb = new StringBuilder();
10+
Queue<TreeNode> q = new LinkedList<>();
11+
q.offer(root);
12+
13+
while (!q.isEmpty()) {
14+
TreeNode node = q.poll();
15+
if (node == null) {
16+
sb.append("null,");
17+
continue;
18+
}
19+
sb.append(node.val).append(",");
20+
q.offer(node.left);
21+
q.offer(node.right);
22+
}
23+
24+
return sb.toString();
25+
}
26+
27+
// Deserialize → String to Tree
28+
public TreeNode deserialize(String data) {
29+
if (data.equals("null")) return null;
30+
31+
String[] vals = data.split(",");
32+
TreeNode root = new TreeNode(Integer.parseInt(vals[0]));
33+
Queue<TreeNode> q = new LinkedList<>();
34+
q.offer(root);
35+
int i = 1;
36+
37+
while (!q.isEmpty()) {
38+
TreeNode curr = q.poll();
39+
if (!vals[i].equals("null")) {
40+
curr.left = new TreeNode(Integer.parseInt(vals[i]));
41+
q.offer(curr.left);
42+
}
43+
i++;
44+
if (!vals[i].equals("null")) {
45+
curr.right = new TreeNode(Integer.parseInt(vals[i]));
46+
q.offer(curr.right);
47+
}
48+
i++;
49+
}
50+
51+
return root;
52+
}
53+
}
54+
55+
Explanation
56+
🔸 serialize() – Tree ko string mein convert karta hai
57+
58+
if (root == null) return "null";
59+
🔹 Agar root hi null hai to null string return karo.
60+
61+
StringBuilder sb = new StringBuilder();
62+
Queue<TreeNode> q = new LinkedList<>();
63+
🔹 Level order ke liye queue banayi.
64+
65+
q.offer(root);
66+
🔹 Root node queue mein daala.
67+
68+
while (!q.isEmpty())
69+
🔹 Jab tak queue khali na ho
70+
71+
TreeNode node = q.poll();
72+
🔹 Queue ka front node nikala.
73+
74+
if (node == null)
75+
sb.append("null,");
76+
🔹 Null node mila toh "null" likh diya.
77+
78+
else:
79+
sb.append(node.val).append(",");
80+
q.offer(node.left);
81+
q.offer(node.right);
82+
🔹 Value likhi aur uske dono children ko queue mein daal diya.
83+
🔸 deserialize() – String ko tree mein wapas convert karta hai
84+
85+
if (data.equals("null")) return null;
86+
🔹 Agar string hi null thi, return null root.
87+
88+
String[] vals = data.split(",");
89+
🔹 Saare values ko comma ke basis par split kiya.
90+
91+
TreeNode root = new TreeNode(Integer.parseInt(vals[0]));
92+
Queue<TreeNode> q = new LinkedList<>();
93+
q.offer(root);
94+
int i = 1;
95+
🔹 Root node create ki aur index 1 se traverse shuru kiya.
96+
97+
while (!q.isEmpty())
98+
🔹 Jab tak queue khali na ho
99+
100+
TreeNode curr = q.poll();
101+
🔹 Queue ka front nikala
102+
103+
if (!vals[i].equals("null"))
104+
curr.left = new TreeNode(Integer.parseInt(vals[i]));
105+
q.offer(curr.left);
106+
107+
i++;
108+
109+
if (!vals[i].equals("null"))
110+
curr.right = new TreeNode(Integer.parseInt(vals[i]));
111+
q.offer(curr.right);
112+
113+
i++;
114+
🔹 Left & right child assign kiya aur queue mein daal diya.
115+
116+
Example:
117+
Input: root = [1,2,3,null,null,4,5]
118+
119+
serialize() → "1,2,3,null,null,4,5,null,null,null,null,"
120+
deserialize() → Tree structure wapas same ban jaata hai
121+
122+
Time and Space Complexity
123+
Operation Time Space
124+
Serialize O(n) O(n)
125+
Deserialize O(n) O(n)
126+
127+
🔗 Facing any issue or want more Java tricks?
128+
https://www.linkedin.com/in/saurabh884095/

0 commit comments

Comments
 (0)