-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubStructureInTree.java
More file actions
51 lines (47 loc) · 1012 Bytes
/
Copy pathSubStructureInTree.java
File metadata and controls
51 lines (47 loc) · 1012 Bytes
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.wg.offerAlgorithm;
/*
* 判断树B是树B的子结构
*
*/
public class SubStructureInTree {
public boolean hasSubTree(BinaryTreeNode root1,BinaryTreeNode root2){
if(root2==null)
return true;
else if(root1==null)
return false;
boolean result=false;
if(root1!=null&&root2!=null){
if(root1.data==root2.data){
result=doesTree1HaveTree2(root1,root2);
}
if(!result){
return hasSubTree(root1.left,root2)||hasSubTree(root1.right,root2);
}
}
return result;
}
/**
* 判断子树是否一样
* @param root1
* @param root2
* @return
*/
public boolean doesTree1HaveTree2(BinaryTreeNode root1,BinaryTreeNode root2){
if(root2==null){
return true;
}else if(root1==null){
return false;
}
if(root1.data!=root2.data){
return false;
}else{
return doesTree1HaveTree2(root1.left,root2.left)&&doesTree1HaveTree2(root1.right,root2.right);
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}