Skip to content

Commit 4ce3026

Browse files
committed
fd
1 parent 87a6409 commit 4ce3026

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@
380380
|557|[Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii/)| [Java](leetcode/solution/src/ReverseWordsInAStringIII.java)|100||
381381
|560|[Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/)| [Java](leetcode/solution/src/SubarraySumEqualsK.java)|70||
382382
|564|[Find the Closest Palindrome](https://leetcode.com/problems/find-the-closest-palindrome/)|[Java](leetcode/solution/src/FindTheClosestPalindrome.java)|80|此题暴力法即可|
383+
|572|[Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/)|[Java](leetcode/solution/src/SubtreeOfAnotherTree.java)|80||
383384
|606|[Construct String from Binary Tree](https://leetcode.com/problems/construct-string-from-binary-tree/)|[Java](leetcode/solution/src/ConstructStringFromBinaryTree.java)|90||
384385
|617|[Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/)|[Java](leetcode/solution/src/MergeTwoBinaryTrees.java)|100|很简单|
385386
|621|[Task Scheduler](https://leetcode.com/problems/task-scheduler/)|[Java](leetcode/solution/src/TaskScheduler.java)|70||
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
public class SubtreeOfAnotherTree {
2+
3+
/**
4+
* 非递归的办法是拼成字符串,看是否是子串
5+
*/
6+
public boolean isSubtree(TreeNode s, TreeNode t) {
7+
return isEqual(s, t) || (s != null && (isSubtree(s.left, t) || isSubtree(s.right, t)));
8+
}
9+
10+
private boolean isEqual(TreeNode s, TreeNode t) {
11+
if (t == null && s == null) {
12+
return true;
13+
}
14+
if (t == null || s == null) {
15+
return false;
16+
}
17+
return s.val == t.val && isEqual(s.left, t.left) && isEqual(s.right, t.right);
18+
}
19+
}

leetcode/src/Main.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,18 @@ public class Main {
66

77
public static class Solution {
88

9-
public List<List<Integer>> pathSum(TreeNode root, int sum) {
10-
List<List<Integer>> result = new ArrayList<>();
11-
helpr(root, sum, new ArrayList<>(), result);
12-
return result;
9+
public boolean isSubtree(TreeNode s, TreeNode t) {
10+
return isEqual(s, t) || (s != null && (isSubtree(s.left, t) || isSubtree(s.right, t)));
1311
}
1412

15-
private void helpr(TreeNode root, int sum, List<Integer> list, List<List<Integer>> result) {
16-
if (root == null) {
17-
return;
13+
private boolean isEqual(TreeNode s, TreeNode t) {
14+
if (t == null && s == null) {
15+
return true;
1816
}
19-
20-
list.add(root.val);
21-
22-
if (root.left == null && root.right == null && root.val == sum) {
23-
result.add(new ArrayList<>(list));
24-
} else {
25-
helpr(root.left, sum - root.val, list, result);
26-
helpr(root.right, sum - root.val, list, result);
17+
if (t == null || s == null) {
18+
return false;
2719
}
28-
29-
list.remove(list.size() - 1);
20+
return s.val == t.val && isEqual(s.left, t.left) && isEqual(s.right, t.right);
3021
}
3122
}
3223

0 commit comments

Comments
 (0)