-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathSameTree.java
157 lines (133 loc) · 3.85 KB
/
SameTree.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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package LeetCodeJava.Recursion;
// https://leetcode.com/problems/same-tree/
/**
* 100. Same Tree
* Solved
* Easy
* Topics
* Companies
* Given the roots of two binary trees p and q, write a function to check if they are the same or not.
*
* Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.
*
*
*
* Example 1:
*
*
* Input: p = [1,2,3], q = [1,2,3]
* Output: true
* Example 2:
*
*
* Input: p = [1,2], q = [1,null,2]
* Output: false
* Example 3:
*
*
* Input: p = [1,2,1], q = [1,1,2]
* Output: false
*
*
* Constraints:
*
* The number of nodes in both trees is in the range [0, 100].
* -104 <= Node.val <= 104
*/
import LeetCodeJava.DataStructure.TreeNode;
import java.util.ArrayDeque;
public class SameTree {
// V0
// IDEA : RECURSION
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) {
return true;
}
if (q == null || p == null){
return false;
}
if (p.val != q.val){
return false;
}
return this.isSameTree(p.left, q.left) &&
this.isSameTree(p.right, q.right);
}
// V0
// IDEA : RECURSION
public boolean isSameTree_0(TreeNode p, TreeNode q) {
if (p == null && q == null){
return true;
}
if ((p == null && q != null) || (p != null && q == null)){
return false;
}
if (p.val != q.val){
return false;
}
//return check(p.left, q.left) && check(p.right, q.right);
return check(p, q); // this one is OK as well
}
private Boolean check(TreeNode t1, TreeNode t2){
if (t1 == null && t2 == null){
return true;
}
if ((t1 == null && t2 != null) || (t1 != null && t2 == null)){
return false;
}
if (t1.val != t2.val){
return false;
}
return check(t1.left, t2.left) && check(t1.right, t2.right);
}
// V1
// IDEA : RECURSION
// https://leetcode.com/problems/same-tree/editorial/
public boolean isSameTre_2(TreeNode p, TreeNode q) {
// p and q are both null
if (p == null && q == null) return true;
// one of p and q is null
if (q == null || p == null) return false;
if (p.val != q.val) return false;
return isSameTree(p.right, q.right) &&
isSameTree(p.left, q.left);
}
// V2
// IDEA : ITERATIVE
// https://leetcode.com/problems/same-tree/editorial/
public boolean check2(TreeNode p, TreeNode q) {
// p and q are null
if (p == null && q == null) return true;
// one of p and q is null
if (q == null || p == null) return false;
if (p.val != q.val) return false;
return true;
}
public boolean isSameTree_3(TreeNode p, TreeNode q) {
if (p == null && q == null) return true;
if (!check(p, q)) return false;
// init deques
ArrayDeque<TreeNode> deqP = new ArrayDeque<TreeNode>();
ArrayDeque<TreeNode> deqQ = new ArrayDeque<TreeNode>();
deqP.addLast(p);
deqQ.addLast(q);
while (!deqP.isEmpty()) {
p = deqP.removeFirst();
q = deqQ.removeFirst();
if (!check2(p, q)) return false;
if (p != null) {
// in Java nulls are not allowed in Deque
if (!check2(p.left, q.left)) return false;
if (p.left != null) {
deqP.addLast(p.left);
deqQ.addLast(q.left);
}
if (!check2(p.right, q.right)) return false;
if (p.right != null) {
deqP.addLast(p.right);
deqQ.addLast(q.right);
}
}
}
return true;
}
}