-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHasSubTree.java
66 lines (55 loc) · 2.03 KB
/
HasSubTree.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
55
56
57
58
59
60
61
62
63
64
65
66
import java.util.ArrayList;
class HasSubTree {
public static void main(String[] args) {
TreeNode root1 = new TreeNode(8);
root1.right = new TreeNode(8);
root1.right.right = new TreeNode(9);
root1.right.right.right = new TreeNode(2);
root1.right.right.right.right = new TreeNode(5);
TreeNode root2 = new TreeNode(8);
root2.right = new TreeNode(9);
root2.right.left= new TreeNode(3);
root2.right.right = new TreeNode(2);
System.out.println(new HasSubTree().hasSubtree(root2, root1));
}
public boolean hasSubtree(TreeNode root1,TreeNode root2) {
ArrayList<TreeNode> l1 = new ArrayList<>();
ArrayList<TreeNode> l2 = new ArrayList<>();
traverse(l1, root1);
traverse(l2, root2);
if (l1.size() == 0 || l2.size() == 0) return false;
if (l1.size() == l2.size()) return equals(root1, root2) || equals(root2, root1);
if (l1.size() < l2.size()) {// 判断 root1 是 root2的子树
for(TreeNode t : l2) {
if (t.val == l1.get(0).val){ // 开始迭代遍历
boolean res = equals(t, l1.get(0));
if (res) return true;
}
}
return false;
} else { // 判断 root2 是 root1 的子树
for(TreeNode t : l1) {
if (t.val == l2.get(0).val){ // 开始迭代遍历
boolean res = equals(l2.get(0), t);
if (res) return true;
}
}
return false;
}
}
void traverse(ArrayList<TreeNode> list, TreeNode root) {
if (root == null) return;
list.add(root);
traverse(list, root.left);
traverse(list, root.right);
}
boolean equals(TreeNode a, TreeNode b) {
if (a == null && null == b) return true;
if (a == null) return false;
if (b == null) return true;
if (a.val == b.val) {
return equals(a.left, b.left) && equals(a.right, b.right);
}
return false;
}
}