Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite719179

Browse files
authored
added solution and test cases for 449 (fishercoder1534#143)
1 parenta33a2bf commite719179

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
importcom.fishercoder.common.classes.TreeNode;
44

5+
importjava.util.Arrays;
56
importjava.util.LinkedList;
67
importjava.util.Queue;
78

@@ -150,4 +151,43 @@ public TreeNode deserialize(String data) {
150151
returnroot;
151152
}
152153
}
154+
publicstaticclassSolution4 {
155+
privatestaticfinalStringNULL_SYMBOL ="X";
156+
privatestaticfinalStringDELIMITER =",";
157+
158+
// Encodes a tree to a single string.
159+
publicStringserialize(TreeNoderoot) {
160+
161+
// If we have a null symbol, encode it to NULL_SYMBOL
162+
if(root ==null)
163+
returnNULL_SYMBOL +DELIMITER;
164+
165+
StringleftSubtree =serialize(root.left);
166+
StringrightSubtree =serialize(root.right);
167+
168+
returnroot.val +DELIMITER +leftSubtree +rightSubtree;
169+
}
170+
171+
// Decodes your encoded data to tree.
172+
publicTreeNodedeserialize(Stringdata) {
173+
174+
Queue<String>nodesLeftToSerialize =newLinkedList<>();
175+
nodesLeftToSerialize.addAll(Arrays.asList(data.split(DELIMITER)));
176+
returndeserializeHelper(nodesLeftToSerialize);
177+
178+
}
179+
privateTreeNodedeserializeHelper(Queue<String>nodesLeft){
180+
181+
// remove the node
182+
StringnodeLeftToSerialize =nodesLeft.poll();
183+
// base case
184+
if(nodeLeftToSerialize.equals(NULL_SYMBOL)){
185+
returnnull;
186+
}
187+
TreeNodenewNode =newTreeNode(Integer.valueOf(nodeLeftToSerialize));
188+
newNode.left =deserializeHelper(nodesLeft);
189+
newNode.right =deserializeHelper(nodesLeft);
190+
returnnewNode;
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
privatestatic_449.Solution1solution1;
1313
privatestatic_449.Solution2solution2;
1414
privatestatic_449.Solution3solution3;
15+
privatestatic_449.Solution4solution4;
1516
privatestaticTreeNodeexpectedRoot;
1617

1718
@BeforeClass
1819
publicstaticvoidsetup() {
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)

[8]ページ先頭

©2009-2025 Movatter.jp