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

add solution and test cases for 449#143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fishercoder1534 merged 1 commit intofishercoder1534:masterfromashmichheda:449
Jan 9, 2021
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletionssrc/main/java/com/fishercoder/solutions/_449.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,7 @@

importcom.fishercoder.common.classes.TreeNode;

importjava.util.Arrays;
importjava.util.LinkedList;
importjava.util.Queue;

Expand DownExpand Up@@ -150,4 +151,43 @@ public TreeNode deserialize(String data) {
returnroot;
}
}
publicstaticclassSolution4 {
privatestaticfinalStringNULL_SYMBOL ="X";
privatestaticfinalStringDELIMITER =",";

// Encodes a tree to a single string.
publicStringserialize(TreeNoderoot) {

// If we have a null symbol, encode it to NULL_SYMBOL
if(root ==null)
returnNULL_SYMBOL +DELIMITER;

StringleftSubtree =serialize(root.left);
StringrightSubtree =serialize(root.right);

returnroot.val +DELIMITER +leftSubtree +rightSubtree;
}

// Decodes your encoded data to tree.
publicTreeNodedeserialize(Stringdata) {

Queue<String>nodesLeftToSerialize =newLinkedList<>();
nodesLeftToSerialize.addAll(Arrays.asList(data.split(DELIMITER)));
returndeserializeHelper(nodesLeftToSerialize);

}
privateTreeNodedeserializeHelper(Queue<String>nodesLeft){

// remove the node
StringnodeLeftToSerialize =nodesLeft.poll();
// base case
if(nodeLeftToSerialize.equals(NULL_SYMBOL)){
returnnull;
}
TreeNodenewNode =newTreeNode(Integer.valueOf(nodeLeftToSerialize));
newNode.left =deserializeHelper(nodesLeft);
newNode.right =deserializeHelper(nodesLeft);
returnnewNode;
}
}
}
4 changes: 4 additions & 0 deletionssrc/test/java/com/fishercoder/_449Test.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,13 +12,15 @@ public class _449Test {
private static _449.Solution1 solution1;
private static _449.Solution2 solution2;
private static _449.Solution3 solution3;
private static _449.Solution4 solution4;
private static TreeNode expectedRoot;

@BeforeClass
public static void setup() {
solution1 = new _449.Solution1();
solution2 = new _449.Solution2();
solution3 = new _449.Solution3();
solution4 = new _449.Solution4();
}

@Before
Expand All@@ -34,6 +36,7 @@ public void test1() {
assertEquals(expectedRoot.toString(), solution1.deserialize(solution1.serialize(expectedRoot)).toString());
assertEquals(expectedRoot.toString(), solution2.deserialize(solution2.serialize(expectedRoot)).toString());
assertEquals(expectedRoot.toString(), solution3.deserialize(solution3.serialize(expectedRoot)).toString());
assertEquals(expectedRoot.toString(), solution4.deserialize(solution4.serialize(expectedRoot)).toString());
}

@Test
Expand All@@ -44,5 +47,6 @@ public void test2() {
assertEquals(expectedRoot.toString(), solution1.deserialize(solution1.serialize(expectedRoot)).toString());
assertEquals(expectedRoot.toString(), solution2.deserialize(solution2.serialize(expectedRoot)).toString());
assertEquals(expectedRoot.toString(), solution3.deserialize(solution3.serialize(expectedRoot)).toString());
assertEquals(expectedRoot.toString(), solution4.deserialize(solution4.serialize(expectedRoot)).toString());
}
}

[8]ページ先頭

©2009-2025 Movatter.jp