forked from damaohongtu/JavaInterview
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSubtreeOfAnotherTree.java
More file actions
39 lines (35 loc) · 894 Bytes
/
SubtreeOfAnotherTree.java
File metadata and controls
39 lines (35 loc) · 894 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
package LeetCode;/**
* @Classname SubtreeOfAnotherTree
* @Description 判断树t是否为树s的子树
* @Date 19-5-30 上午10:57
* @Created by mao<tianmao818@qq.com>
*/
public class SubtreeOfAnotherTree {
public boolean isSubtree(TreeNode s, TreeNode t) {
boolean ans=false;
if (s!=null&&t!=null){
if(s.val==t.val){
ans=helper(s,t);
}
if(!ans){
ans=isSubtree(s.left,t);
}
if(!ans){
ans=isSubtree(s.right,t);
}
}
return ans;
}
public boolean helper(TreeNode s,TreeNode t){
if(t==null){
return true;
}
if (s==null){
return false;
}
if(s.val!=t.val){
return false;
}
return helper(s.left,t.left)&&helper(s.right,t.right);
}
}