-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubtreeOfAnotherTree_572.java
More file actions
44 lines (37 loc) · 995 Bytes
/
Copy pathSubtreeOfAnotherTree_572.java
File metadata and controls
44 lines (37 loc) · 995 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
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public class SubtreeOfAnotherTree_572 {
public boolean isSame(TreeNode s, TreeNode t)
{
if (s==null && t==null)
return true;
else if (s==null || t==null)
return false;
else if ( s.val == t.val )
{
return isSame(s.left, t.left) && isSame(s.right, t.right);
}
else
return false;
}
public boolean isMySubTree(TreeNode s, TreeNode t)
{
if (s == null)
return false;
if (isSame(s, t))
return true;
return isMySubTree(s.left, t) || isMySubTree(s.right, t);
}
public boolean isSubtree(TreeNode s, TreeNode t) {
if (s == null || t == null)
return true;
return isMySubTree(s, t) || isMySubTree(t, s);
}
public static void main(String[] args)
{
}
} // LowestCommonAncestorOfBST_235