42
42
43
43
<!-- 这里可写通用的实现逻辑 -->
44
44
45
- 从上到下搜索,找到第一个值位于 ` [p, q] ` 之间的结点即可。既可以用迭代实现,也可以用递归实现。
45
+ ** 方法一:迭代或递归**
46
+
47
+ 从上到下搜索,找到第一个值位于 $[ p.val, q.val] $ 之间的结点即可。
48
+
49
+ 既可以用迭代实现,也可以用递归实现。
50
+
51
+ 迭代的时间复杂度为 $O(n)$,空间复杂度为 $O(1)$。
52
+
53
+ 递归的时间复杂度为 $O(n)$,空间复杂度为 $O(n)$。
46
54
47
55
<!-- tabs:start -->
48
56
@@ -65,7 +73,7 @@ class Solution:
65
73
def lowestCommonAncestor (
66
74
self , root : ' TreeNode' , p : ' TreeNode' , q : ' TreeNode'
67
75
) -> ' TreeNode' :
68
- while root :
76
+ while 1 :
69
77
if root.val < min (p.val, q.val):
70
78
root = root.right
71
79
elif root.val > max (p.val, q.val):
@@ -84,10 +92,11 @@ class Solution:
84
92
# self.left = None
85
93
# self.right = None
86
94
95
+
87
96
class Solution :
88
- def lowestCommonAncestor (self , root : ' TreeNode ' , p : ' TreeNode ' , q : ' TreeNode ' ) -> ' TreeNode ' :
89
- if root is None :
90
- return None
97
+ def lowestCommonAncestor (
98
+ self , root : ' TreeNode ' , p : ' TreeNode ' , q : ' TreeNode '
99
+ ) -> ' TreeNode ' :
91
100
if root.val < min (p.val, q.val):
92
101
return self .lowestCommonAncestor(root.right, p, q)
93
102
if root.val > max (p.val, q.val):
@@ -114,12 +123,15 @@ class Solution:
114
123
115
124
class Solution {
116
125
public TreeNode lowestCommonAncestor (TreeNode root , TreeNode p , TreeNode q ) {
117
- while (root != null ) {
118
- if (root. val < p. val && root. val < q. val) root = root. right;
119
- else if (root. val > p. val && root. val > q. val) root = root. left;
120
- else return root;
126
+ while (true ) {
127
+ if (root. val < Math . min(p. val, q. val)) {
128
+ root = root. right;
129
+ } else if (root. val > Math . max(p. val, q. val)) {
130
+ root = root. left;
131
+ } else {
132
+ return root;
133
+ }
121
134
}
122
- return root;
123
135
}
124
136
}
125
137
```
@@ -139,78 +151,73 @@ class Solution {
139
151
140
152
class Solution {
141
153
public TreeNode lowestCommonAncestor (TreeNode root , TreeNode p , TreeNode q ) {
142
- if (root == null ) return null ;
143
- if (root. val < p. val && root. val < q. val) return lowestCommonAncestor(root. right, p, q);
144
- if (root. val > p. val && root. val > q. val) return lowestCommonAncestor(root. left, p, q);
154
+ if (root. val < Math . min(p. val, q. val)) {
155
+ return lowestCommonAncestor(root. right, p, q);
156
+ }
157
+ if (root. val > Math . max(p. val, q. val)) {
158
+ return lowestCommonAncestor(root. left, p, q);
159
+ }
145
160
return root;
146
161
}
147
162
}
148
163
```
149
164
150
- ### ** TypeScript **
165
+ ### ** C++ **
151
166
152
167
迭代:
153
168
154
- ``` ts
169
+ ``` cpp
155
170
/* *
156
171
* Definition for a binary tree node.
157
- * class TreeNode {
158
- * val: number
159
- * left: TreeNode | null
160
- * right: TreeNode | null
161
- * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
162
- * this.val = (val===undefined ? 0 : val)
163
- * this.left = (left===undefined ? null : left)
164
- * this.right = (right===undefined ? null : right)
165
- * }
166
- * }
172
+ * struct TreeNode {
173
+ * int val;
174
+ * TreeNode *left;
175
+ * TreeNode *right;
176
+ * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
177
+ * };
167
178
*/
168
179
169
- function lowestCommonAncestor(
170
- root : TreeNode | null ,
171
- p : TreeNode | null ,
172
- q : TreeNode | null ,
173
- ): TreeNode | null {
174
- while (root ) {
175
- if (root .val > p .val && root .val > q .val ) {
176
- root = root .left ;
177
- } else if (root .val < p .val && root .val < q .val ) {
178
- root = root .right ;
179
- } else {
180
- return root ;
180
+ class Solution {
181
+ public:
182
+ TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
183
+ while (1) {
184
+ if (root->val < min(p->val, q->val)) {
185
+ root = root->right;
186
+ } else if (root->val > max(p->val, q->val)) {
187
+ root = root->left;
188
+ } else {
189
+ return root;
190
+ }
181
191
}
182
192
}
183
- }
193
+ };
184
194
```
185
195
186
196
递归:
187
197
188
- ``` ts
198
+ ```cpp
189
199
/**
190
200
* Definition for a binary tree node.
191
- * class TreeNode {
192
- * val: number
193
- * left: TreeNode | null
194
- * right: TreeNode | null
195
- * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
196
- * this.val = (val===undefined ? 0 : val)
197
- * this.left = (left===undefined ? null : left)
198
- * this.right = (right===undefined ? null : right)
199
- * }
200
- * }
201
+ * struct TreeNode {
202
+ * int val;
203
+ * TreeNode *left;
204
+ * TreeNode *right;
205
+ * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
206
+ * };
201
207
*/
202
208
203
- function lowestCommonAncestor(
204
- root : TreeNode | null ,
205
- p : TreeNode | null ,
206
- q : TreeNode | null ,
207
- ): TreeNode | null {
208
- if (root .val > p .val && root .val > q .val )
209
- return lowestCommonAncestor (root .left , p , q );
210
- if (root .val < p .val && root .val < q .val )
211
- return lowestCommonAncestor (root .right , p , q );
212
- return root ;
213
- }
209
+ class Solution {
210
+ public:
211
+ TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
212
+ if (root->val < min(p->val, q->val)) {
213
+ return lowestCommonAncestor(root->right, p, q);
214
+ }
215
+ if (root->val > max(p->val, q->val)) {
216
+ return lowestCommonAncestor(root->left, p, q);
217
+ }
218
+ return root;
219
+ }
220
+ };
214
221
```
215
222
216
223
### ** Go**
@@ -228,16 +235,15 @@ function lowestCommonAncestor(
228
235
*/
229
236
230
237
func lowestCommonAncestor (root , p , q *TreeNode ) *TreeNode {
231
- for root != nil {
232
- if root.Val > p.Val && root.Val > q.Val {
233
- root = root.Left
234
- } else if root.Val < p.Val && root.Val < q.Val {
238
+ for {
239
+ if root.Val < p.Val && root.Val < q.Val {
235
240
root = root.Right
241
+ } else if root.Val > p.Val && root.Val > q.Val {
242
+ root = root.Left
236
243
} else {
237
244
return root
238
245
}
239
246
}
240
- return nil
241
247
}
242
248
```
243
249
@@ -254,72 +260,86 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
254
260
*/
255
261
256
262
func lowestCommonAncestor (root , p , q *TreeNode ) *TreeNode {
257
- if root == nil {
258
- return root
259
- }
260
- if root.Val < p.Val && root.Val < q.Val {
261
- return lowestCommonAncestor (root.Right , p, q)
262
- }
263
- if root.Val > p.Val && root.Val > q.Val {
264
- return lowestCommonAncestor (root.Left , p, q)
265
- }
266
- return root
263
+ if root.Val < p.Val && root.Val < q.Val {
264
+ return lowestCommonAncestor (root.Right , p, q)
265
+ }
266
+ if root.Val > p.Val && root.Val > q.Val {
267
+ return lowestCommonAncestor (root.Left , p, q)
268
+ }
269
+ return root
267
270
}
268
271
```
269
272
270
- ### ** C++ **
273
+ ### ** TypeScript **
271
274
272
275
迭代:
273
276
274
- ``` cpp
277
+ ``` ts
275
278
/**
276
279
* Definition for a binary tree node.
277
- * struct TreeNode {
278
- * int val;
279
- * TreeNode *left;
280
- * TreeNode *right;
281
- * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
282
- * };
280
+ * class TreeNode {
281
+ * val: number
282
+ * left: TreeNode | null
283
+ * right: TreeNode | null
284
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
285
+ * this.val = (val===undefined ? 0 : val)
286
+ * this.left = (left===undefined ? null : left)
287
+ * this.right = (right===undefined ? null : right)
288
+ * }
289
+ * }
283
290
*/
284
291
285
- class Solution {
286
- public:
287
- TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
288
- while (root) {
289
- if (root->val < p->val && root->val < q->val)
290
- root = root->right;
291
- else if (root->val > p->val && root->val > q->val)
292
- root = root->left;
293
- else
294
- return root;
292
+ function lowestCommonAncestor(
293
+ root : TreeNode | null ,
294
+ p : TreeNode | null ,
295
+ q : TreeNode | null ,
296
+ ): TreeNode | null {
297
+ while (root ) {
298
+ if (root .val > p .val && root .val > q .val ) {
299
+ root = root .left ;
300
+ } else if (root .val < p .val && root .val < q .val ) {
301
+ root = root .right ;
302
+ } else {
303
+ return root ;
295
304
}
296
- return root;
297
305
}
298
- };
306
+ }
299
307
```
300
308
301
309
递归:
302
310
303
- ```cpp
311
+ ``` ts
304
312
/**
305
313
* Definition for a binary tree node.
306
- * struct TreeNode {
307
- * int val;
308
- * TreeNode *left;
309
- * TreeNode *right;
310
- * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
311
- * };
314
+ * class TreeNode {
315
+ * val: number
316
+ * left: TreeNode | null
317
+ * right: TreeNode | null
318
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
319
+ * this.val = (val===undefined ? 0 : val)
320
+ * this.left = (left===undefined ? null : left)
321
+ * this.right = (right===undefined ? null : right)
322
+ * }
323
+ * }
312
324
*/
313
325
314
- class Solution {
315
- public:
316
- TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
317
- if (!root) return root;
318
- if (root->val < p->val && root->val < q->val) return lowestCommonAncestor(root->right, p, q);
319
- if (root->val > p->val && root->val > q->val) return lowestCommonAncestor(root->left, p, q);
320
- return root;
321
- }
322
- };
326
+ function lowestCommonAncestor(
327
+ root : TreeNode | null ,
328
+ p : TreeNode | null ,
329
+ q : TreeNode | null ,
330
+ ): TreeNode | null {
331
+ if (root .val > p .val && root .val > q .val )
332
+ return lowestCommonAncestor (root .left , p , q );
333
+ if (root .val < p .val && root .val < q .val )
334
+ return lowestCommonAncestor (root .right , p , q );
335
+ return root ;
336
+ }
337
+ ```
338
+
339
+ ### ** ...**
340
+
341
+ ```
342
+
323
343
```
324
344
325
345
<!-- tabs: end -->
0 commit comments