-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSubtreeOfAnotherTree.java
More file actions
57 lines (42 loc) · 1.68 KB
/
Copy pathSubtreeOfAnotherTree.java
File metadata and controls
57 lines (42 loc) · 1.68 KB
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
55
56
57
// Given two non-empty binary trees s and t, check whether tree t has exactly
// the same structure and node values with a subtree of s.
// A subtree of s is a tree consists of a node in s and all of this node's descendants.
// The tree s could also be considered as a subtree of itself.
// See: https://leetcode.com/problems/subtree-of-another-tree/
package leetcode.tree;
import static leetcode.util.tree.BinTreeUtil.initTree;
import leetcode.util.tree.TreeNode;
public class SubtreeOfAnotherTree {
public boolean isSubtree(TreeNode s, TreeNode t) {
if (s == null) {
return false;
}
if (areSameTree(s, t)) {
return true;
}
return isSubtree(s.left, t) || isSubtree(s.right, t);
}
private boolean areSameTree(TreeNode t1, TreeNode t2) {
if (t1 == null && t2 == null) {
return true;
}
if (t1 == null && t2 != null || t1 != null && t2 == null) {
return false;
}
if (t1.val != t2.val) {
return false;
}
return areSameTree(t1.left, t2.left) && areSameTree(t1.right, t2.right);
}
public static void main(String[] args) {
SubtreeOfAnotherTree sln = new SubtreeOfAnotherTree();
TreeNode s1 = initTree(3, 4, 5, 1, 2);
TreeNode t1 = initTree(4, 1, 2);
TreeNode s2 = initTree(4, 1, 2, null, null, 0);
// TreeNode t3 = initTree(4, 1, 2);
// System.out.println(sln.areSameTree(t1, t2));
// System.out.println(sln.areSameTree(t1, t3));
System.out.println(sln.isSubtree(s1, t1));
System.out.println(sln.isSubtree(s2, t1));
}
}