-
Notifications
You must be signed in to change notification settings - Fork 0
/
SubtreeOfAnotherTree.java
54 lines (44 loc) · 1.43 KB
/
SubtreeOfAnotherTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package solutions;
import datastructure.TreeNode;
// [Problem] https://leetcode.com/problems/subtree-of-another-tree/
class SubtreeOfAnotherTree {
// test
public static void main(String[] args) {
SubtreeOfAnotherTree solution = new SubtreeOfAnotherTree();
// Given tree s:
// 3
// / \
// 4 5
// / \
// 1 2
TreeNode s = new TreeNode(3,
new TreeNode(4, new TreeNode(1), new TreeNode(2)), new TreeNode(5));
// Given tree t:
// 4
// / \
// 1 2
TreeNode t = new TreeNode(4, new TreeNode(1), new TreeNode(2));
boolean expectedOutput = true;
boolean actualOutput = solution.isSubtree(s, t);
System.out.println("Test passed? " + (expectedOutput == actualOutput));
}
// Preorder Traversal and Recursion - O(m * n) time, O(n) space
public boolean isSubtree(TreeNode s, TreeNode t) {
if (s == null) {
return false;
}
return isSameTree(s, t) || isSubtree(s.left, t) || isSubtree(s.right, t);
}
private boolean isSameTree(TreeNode s, TreeNode t) {
if (s == null && t == null) {
return true;
}
if (s == null || t == null) {
return false;
}
if (s.val != t.val) {
return false;
}
return isSameTree(s.left, t.left) && isSameTree(s.right, t.right);
}
}