Skip to content

Commit e719179

Browse files
authored
added solution and test cases for 449 (fishercoder1534#143)
1 parent a33a2bf commit e719179

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/main/java/com/fishercoder/solutions/_449.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fishercoder.common.classes.TreeNode;
44

5+
import java.util.Arrays;
56
import java.util.LinkedList;
67
import java.util.Queue;
78

@@ -150,4 +151,43 @@ public TreeNode deserialize(String data) {
150151
return root;
151152
}
152153
}
154+
public static class Solution4 {
155+
private static final String NULL_SYMBOL = "X";
156+
private static final String DELIMITER = ",";
157+
158+
// Encodes a tree to a single string.
159+
public String serialize(TreeNode root) {
160+
161+
// If we have a null symbol, encode it to NULL_SYMBOL
162+
if(root == null)
163+
return NULL_SYMBOL + DELIMITER;
164+
165+
String leftSubtree = serialize(root.left);
166+
String rightSubtree = serialize(root.right);
167+
168+
return root.val + DELIMITER + leftSubtree + rightSubtree;
169+
}
170+
171+
// Decodes your encoded data to tree.
172+
public TreeNode deserialize(String data) {
173+
174+
Queue<String> nodesLeftToSerialize = new LinkedList<>();
175+
nodesLeftToSerialize.addAll(Arrays.asList(data.split(DELIMITER)));
176+
return deserializeHelper(nodesLeftToSerialize);
177+
178+
}
179+
private TreeNode deserializeHelper(Queue<String> nodesLeft){
180+
181+
// remove the node
182+
String nodeLeftToSerialize = nodesLeft.poll();
183+
// base case
184+
if(nodeLeftToSerialize.equals(NULL_SYMBOL)){
185+
return null;
186+
}
187+
TreeNode newNode = new TreeNode(Integer.valueOf(nodeLeftToSerialize));
188+
newNode.left = deserializeHelper(nodesLeft);
189+
newNode.right = deserializeHelper(nodesLeft);
190+
return newNode;
191+
}
192+
}
153193
}

src/test/java/com/fishercoder/_449Test.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ public class _449Test {
1212
private static _449.Solution1 solution1;
1313
private static _449.Solution2 solution2;
1414
private static _449.Solution3 solution3;
15+
private static _449.Solution4 solution4;
1516
private static TreeNode expectedRoot;
1617

1718
@BeforeClass
1819
public static void setup() {
1920
solution1 = new _449.Solution1();
2021
solution2 = new _449.Solution2();
2122
solution3 = new _449.Solution3();
23+
solution4 = new _449.Solution4();
2224
}
2325

2426
@Before
@@ -34,6 +36,7 @@ public void test1() {
3436
assertEquals(expectedRoot.toString(), solution1.deserialize(solution1.serialize(expectedRoot)).toString());
3537
assertEquals(expectedRoot.toString(), solution2.deserialize(solution2.serialize(expectedRoot)).toString());
3638
assertEquals(expectedRoot.toString(), solution3.deserialize(solution3.serialize(expectedRoot)).toString());
39+
assertEquals(expectedRoot.toString(), solution4.deserialize(solution4.serialize(expectedRoot)).toString());
3740
}
3841

3942
@Test
@@ -44,5 +47,6 @@ public void test2() {
4447
assertEquals(expectedRoot.toString(), solution1.deserialize(solution1.serialize(expectedRoot)).toString());
4548
assertEquals(expectedRoot.toString(), solution2.deserialize(solution2.serialize(expectedRoot)).toString());
4649
assertEquals(expectedRoot.toString(), solution3.deserialize(solution3.serialize(expectedRoot)).toString());
50+
assertEquals(expectedRoot.toString(), solution4.deserialize(solution4.serialize(expectedRoot)).toString());
4751
}
4852
}

0 commit comments

Comments
 (0)