-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolutionJZ16.java
More file actions
77 lines (69 loc) · 1.85 KB
/
SolutionJZ16.java
File metadata and controls
77 lines (69 loc) · 1.85 KB
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
67
68
69
70
71
72
73
74
75
76
package com.company;
public class SolutionJZ16 {
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
class SolutionJZ17 {
public boolean HasSubtree(TreeNode root1,TreeNode root2) {
boolean result= false;
if(root1!=null&&root2!=null){
if(equalNum(root1.val,root2.val)){
result=doesEqual(root1,root2);
}
if(!result){
result=HasSubtree(root1.left,root2);
}
if(!result){
result=HasSubtree(root1.right,root2);
}
}
return result;
}
boolean doesEqual(TreeNode root1,TreeNode root2){
if(root2==null)
return true;
if(root1==null)
return false;
if(!equalNum(root1.val,root2.val)){
return false;
}
return doesEqual(root1.left,root2.left)&&doesEqual(root1.right,root2.right);
}
boolean equalNum(int num1,int num2){
if(num1==num2)
return true;
else
return false;
}
}
public ListNode Merge(ListNode list1,ListNode list2) {
ListNode p=new ListNode(0);
ListNode temp=p;
while(list1!=null&&list2!=null){
if(list1.val<list2.val){
temp.next=list1;
list1=list1.next;
}
else {
temp.next=list2;
list2=list2.next;
}
temp=temp.next;
}
if(list1==null){
temp.next=list2;
}
else{
temp.next=list1;
}
p=p.next;
return p;
}
}