-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q26_树的子结构.java
51 lines (45 loc) · 1.48 KB
/
Q26_树的子结构.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
package com.algorithm.demo.剑指Offer;
import com.algorithm.demo.tree.TreeNode;
/**
* 输入两颗二叉树A和B,判断B是不是A的字结构。
* <p>
* 要查找树A是否存在和树B结构一样的紫薯,分成两步:
* 1.在树A中找到和树B的跟节点的值一样的节点R
* 2.判断树A中以R为根节点的子树是不是包含和树B一样的结构
*/
public class Q26_树的子结构 {
public boolean HasSubtree(TreeNode rootA, TreeNode rootB) {
boolean result = false;
if (rootA != null && rootB != null) {
if (!Equal(rootA.val, rootB.val)) {
result = DoesTreeHaveTree(rootA, rootB);
}
if (!result) {
result = HasSubtree(rootA.left, rootB);
}
if (!result) {
result = HasSubtree(rootA.right, rootB);
}
}
return result;
}
private boolean DoesTreeHaveTree(TreeNode rootA, TreeNode rootB) {
if (rootB == null) {
return true;
}
if (rootA == null) {
return false;
}
if (!Equal(rootA.val, rootB.val)) {
return false;
}
return DoesTreeHaveTree(rootA.left, rootB.left) && DoesTreeHaveTree(rootA.right, rootB.right);
}
public boolean Equal(double num1, double num2) {
if ((num1 - num2 > -0.0000001) && (num1 - num2 < 0.0000001)) {
return true;
} else {
return false;
}
}
}