42
42
43
43
<!-- 这里可写通用的实现逻辑 -->
44
44
45
- 从上到下搜索,找到第一个值位于 ` [p, q] ` 之间的结点即可。既可以用迭代实现,也可以用递归实现。
45
+ ** 方法一:一次遍历**
46
+
47
+ 从上到下遍历二叉树,找到第一个值位于 $[ p.val,.. q.val] $ 之间的结点即可。既可以用迭代实现,也可以用递归实现。
48
+
49
+ 时间复杂度 $O(n)$,其中 $n$ 是二叉树的结点数。空间复杂度方面,迭代实现的空间复杂度为 $O(1)$,递归实现的空间复杂度为 $O(n)$。
46
50
47
51
<!-- tabs:start -->
48
52
49
53
### ** Python3**
50
54
51
55
<!-- 这里可写当前语言的特殊实现逻辑 -->
52
56
53
- #### 迭代法
54
-
55
57
``` python
56
58
# Definition for a binary tree node.
57
59
# class TreeNode:
@@ -65,9 +67,7 @@ class Solution:
65
67
def lowestCommonAncestor (
66
68
self , root : TreeNode, p : TreeNode, q : TreeNode
67
69
) -> TreeNode:
68
- if p == q:
69
- return p
70
- while root:
70
+ while 1 :
71
71
if root.val < p.val and root.val < q.val:
72
72
root = root.right
73
73
elif root.val > p.val and root.val > q.val:
@@ -76,8 +76,6 @@ class Solution:
76
76
return root
77
77
```
78
78
79
- #### 递归法
80
-
81
79
``` python
82
80
# Definition for a binary tree node.
83
81
# class TreeNode:
@@ -111,10 +109,7 @@ class Solution:
111
109
*/
112
110
class Solution {
113
111
public TreeNode lowestCommonAncestor (TreeNode root , TreeNode p , TreeNode q ) {
114
- if (p == q) {
115
- return p;
116
- }
117
- while (root != null ) {
112
+ while (true ) {
118
113
if (root. val < p. val && root. val < q. val) {
119
114
root = root. right;
120
115
} else if (root. val > p. val && root. val > q. val) {
@@ -123,11 +118,131 @@ class Solution {
123
118
return root;
124
119
}
125
120
}
126
- return null ;
127
121
}
128
122
}
129
123
```
130
124
125
+ ``` java
126
+ /**
127
+ * Definition for a binary tree node.
128
+ * public class TreeNode {
129
+ * int val;
130
+ * TreeNode left;
131
+ * TreeNode right;
132
+ * TreeNode(int x) { val = x; }
133
+ * }
134
+ */
135
+ class Solution {
136
+ public TreeNode lowestCommonAncestor (TreeNode root , TreeNode p , TreeNode q ) {
137
+ if (root. val < p. val && root. val < q. val) {
138
+ return lowestCommonAncestor(root. right, p, q);
139
+ }
140
+ if (root. val > p. val && root. val > q. val) {
141
+ return lowestCommonAncestor(root. left, p, q);
142
+ }
143
+ return root;
144
+ }
145
+ }
146
+ ```
147
+
148
+ ### ** C++**
149
+
150
+ ``` cpp
151
+ /* *
152
+ * Definition for a binary tree node.
153
+ * struct TreeNode {
154
+ * int val;
155
+ * TreeNode *left;
156
+ * TreeNode *right;
157
+ * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
158
+ * };
159
+ */
160
+ class Solution {
161
+ public:
162
+ TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
163
+ if (root->val < p->val && root->val < q->val) {
164
+ return lowestCommonAncestor(root->right, p, q);
165
+ }
166
+ if (root->val > p->val && root->val > q->val) {
167
+ return lowestCommonAncestor(root->left, p, q);
168
+ }
169
+ return root;
170
+ }
171
+ };
172
+ ```
173
+
174
+ ```cpp
175
+ /**
176
+ * Definition for a binary tree node.
177
+ * struct TreeNode {
178
+ * int val;
179
+ * TreeNode *left;
180
+ * TreeNode *right;
181
+ * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
182
+ * };
183
+ */
184
+ class Solution {
185
+ public:
186
+ TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
187
+ while (1) {
188
+ if (root->val < p->val && root->val < q->val) {
189
+ root = root->right;
190
+ } else if (root->val > p->val && root->val > q->val) {
191
+ root = root->left;
192
+ } else {
193
+ return root;
194
+ }
195
+ }
196
+ }
197
+ };
198
+ ```
199
+
200
+ ### ** Go**
201
+
202
+ ``` go
203
+ /* *
204
+ * Definition for a binary tree node.
205
+ * type TreeNode struct {
206
+ * Val int
207
+ * Left *TreeNode
208
+ * Right *TreeNode
209
+ * }
210
+ */
211
+
212
+ func lowestCommonAncestor (root , p , q *TreeNode ) *TreeNode {
213
+ if root.Val < p.Val && root.Val < q.Val {
214
+ return lowestCommonAncestor (root.Right , p, q)
215
+ }
216
+ if root.Val > p.Val && root.Val > q.Val {
217
+ return lowestCommonAncestor (root.Left , p, q)
218
+ }
219
+ return root
220
+ }
221
+ ```
222
+
223
+ ``` go
224
+ /* *
225
+ * Definition for a binary tree node.
226
+ * type TreeNode struct {
227
+ * Val int
228
+ * Left *TreeNode
229
+ * Right *TreeNode
230
+ * }
231
+ */
232
+
233
+ func lowestCommonAncestor (root , p , q *TreeNode ) *TreeNode {
234
+ for {
235
+ if root.Val < p.Val && root.Val < q.Val {
236
+ root = root.Right
237
+ } else if root.Val > p.Val && root.Val > q.Val {
238
+ root = root.Left
239
+ } else {
240
+ return root
241
+ }
242
+ }
243
+ }
244
+ ```
245
+
131
246
### ** JavaScript**
132
247
133
248
``` js
@@ -145,8 +260,6 @@ class Solution {
145
260
* @return {TreeNode}
146
261
*/
147
262
var lowestCommonAncestor = function (root , p , q ) {
148
- // 递归
149
- if (! root) return null ;
150
263
if (root .val < p .val && root .val < q .val ) {
151
264
return lowestCommonAncestor (root .right , p, q);
152
265
} else if (root .val > p .val && root .val > q .val ) {
@@ -158,8 +271,6 @@ var lowestCommonAncestor = function (root, p, q) {
158
271
159
272
### ** TypeScript**
160
273
161
- 递归:
162
-
163
274
``` ts
164
275
/**
165
276
* Definition for a binary tree node.
@@ -192,8 +303,6 @@ function lowestCommonAncestor(
192
303
}
193
304
```
194
305
195
- 循环:
196
-
197
306
``` ts
198
307
/**
199
308
* Definition for a binary tree node.
0 commit comments