Skip to content

Commit 03e6857

Browse files
fix build
1 parent a5f3161 commit 03e6857

File tree

2 files changed

+1
-36
lines changed

2 files changed

+1
-36
lines changed

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

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,34 +41,6 @@
4141
public class _331 {
4242

4343
public static class Solution1 {
44-
/**
45-
* credit: https://discuss.leetcode.com/topic/35976/7-lines-easy-java-solution Some used stack.
46-
* Some used the depth of a stack. Here I use a different perspective. In a binary tree, if we
47-
* consider null as leaves, then all non-null node provides 2 outdegree and 1 indegree (2
48-
* children and 1 parent), except root all null node provides 0 outdegree and 1 indegree (0
49-
* child and 1 parent). Suppose we try to build this tree. During building, we record the
50-
* difference between out degree and in degree diff = outdegree - indegree. When the next node
51-
* comes, we then decrease diff by 1, because the node provides an in degree. If the node is not
52-
* null, we increase diff by 2, because it provides two out degrees. If a serialization is
53-
* correct, diff should never be negative and diff will be zero when finished.
54-
*/
55-
public boolean isValidSerialization(String preorder) {
56-
String[] pre = preorder.split(",");
57-
int diff = 1;
58-
for (String each : pre) {
59-
if (diff < 0) {
60-
return false;
61-
}
62-
diff--;
63-
if (!each.equals("#")) {
64-
diff += 2;
65-
}
66-
}
67-
return diff == 0;
68-
}
69-
}
70-
71-
public static class Solution2 {
7244
public boolean isValidSerialization(String preorder) {
7345
/**Idea: we keep inserting the string into the stack, if it's a number, we just push it onto the stack;
7446
* if it's a "#", we see if the top of the stack is a "#" or not,

src/test/java/com/fishercoder/_331Test.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,34 @@
88

99
public class _331Test {
1010
private static _331.Solution1 solution1;
11-
private static _331.Solution2 solution2;
1211

1312
@BeforeClass
1413
public static void setup() {
1514
solution1 = new _331.Solution1();
16-
solution2 = new _331.Solution2();
1715
}
1816

1917
@Test
2018
public void test1() {
2119
assertEquals(true, solution1.isValidSerialization("9,3,4,#,#,1,#,#,2,#,6,#,#"));
22-
assertEquals(true, solution2.isValidSerialization("9,3,4,#,#,1,#,#,2,#,6,#,#"));
2320
}
2421

2522
@Test
2623
public void test2() {
2724
assertEquals(false, solution1.isValidSerialization("1,#"));
28-
assertEquals(false, solution2.isValidSerialization("1,#"));
2925
}
3026

3127
@Test
3228
public void test3() {
3329
assertEquals(false, solution1.isValidSerialization("9,#,#,1"));
34-
assertEquals(false, solution2.isValidSerialization("9,#,#,1"));
3530
}
3631

3732
@Test
3833
public void test4() {
3934
assertEquals(false, solution1.isValidSerialization("1"));
40-
assertEquals(false, solution2.isValidSerialization("1"));
4135
}
4236

4337
@Test
4438
public void test5() {
45-
assertEquals(true, solution1.isValidSerialization("#,7,6,9,#,#,#"));
46-
assertEquals(true, solution2.isValidSerialization("#,7,6,9,#,#,#"));
39+
assertEquals(false, solution1.isValidSerialization("#,7,6,9,#,#,#"));
4740
}
4841
}

0 commit comments

Comments
 (0)