Skip to content

Commit 65d6adf

Browse files
update 572
1 parent 800e9e1 commit 65d6adf

File tree

2 files changed

+5
-46
lines changed
  • src
    • main/java/com/fishercoder/solutions/firstthousand
    • test/java/com/fishercoder/firstthousand

2 files changed

+5
-46
lines changed

src/main/java/com/fishercoder/solutions/firstthousand/_572.java

-37
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,6 @@
55
public class _572 {
66

77
public static class Solution1 {
8-
public boolean isSubtree(TreeNode s, TreeNode t) {
9-
if (s == null && t == null) {
10-
return true;
11-
}
12-
boolean isSubTree = false;
13-
if (s != null && t != null && s.val == t.val) {
14-
isSubTree = isSameTree(s, t);
15-
}
16-
if (isSubTree) {
17-
return true;
18-
}
19-
boolean isSubTreeLeft = false;
20-
if (s.left != null) {
21-
isSubTreeLeft = isSubtree(s.left, t);
22-
}
23-
if (isSubTreeLeft) {
24-
return true;
25-
}
26-
boolean isSubTreeRight = false;
27-
if (s.right != null) {
28-
isSubTreeRight = isSubtree(s.right, t);
29-
}
30-
if (isSubTreeRight) {
31-
return true;
32-
}
33-
return false;
34-
}
35-
36-
private boolean isSameTree(TreeNode p, TreeNode q) {
37-
if (p == null || q == null) {
38-
return p == q;
39-
}
40-
return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
41-
}
42-
}
43-
44-
public static class Solution2 {
458
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
469
if (root == null) {
4710
return false;

src/test/java/com/fishercoder/firstthousand/_572Test.java

+5-9
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,22 @@
33
import com.fishercoder.common.classes.TreeNode;
44
import com.fishercoder.common.utils.TreeUtils;
55
import com.fishercoder.solutions.firstthousand._572;
6-
import org.junit.BeforeClass;
7-
import org.junit.Test;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
88

99
import java.util.Arrays;
1010

11-
import static org.junit.Assert.assertEquals;
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
1212

1313
public class _572Test {
1414
private static _572.Solution1 solution1;
15-
private static _572.Solution2 solution2;
1615
private static boolean expected;
1716
private static TreeNode root;
1817
private static TreeNode subRoot;
1918

20-
@BeforeClass
21-
public static void setup() {
19+
@BeforeEach
20+
public void setup() {
2221
solution1 = new _572.Solution1();
23-
solution2 = new _572.Solution2();
2422
}
2523

2624
@Test
@@ -30,7 +28,6 @@ public void test1() {
3028
subRoot = TreeUtils.constructBinaryTree(Arrays.asList(4, 1, 2));
3129
expected = true;
3230
assertEquals(expected, solution1.isSubtree(root, subRoot));
33-
assertEquals(expected, solution2.isSubtree(root, subRoot));
3431
}
3532

3633
@Test
@@ -40,7 +37,6 @@ public void test2() {
4037
subRoot = TreeUtils.constructBinaryTree(Arrays.asList(4, 1, 2));
4138
expected = false;
4239
assertEquals(expected, solution1.isSubtree(root, subRoot));
43-
assertEquals(expected, solution2.isSubtree(root, subRoot));
4440
}
4541

4642
}

0 commit comments

Comments
 (0)