Skip to content

Commit a1c763e

Browse files
committed
feat: add solutions to lc problem: No.0235
No.0235.Lowest Common Ancestor of a Binary Search Tree
1 parent 254279a commit a1c763e

File tree

9 files changed

+266
-242
lines changed

9 files changed

+266
-242
lines changed

solution/0200-0299/0234.Palindrome Linked List/README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@
6060
# self.val = val
6161
# self.next = next
6262
class Solution:
63-
def isPalindrome(self, head: ListNode) -> bool:
64-
if head is None or head.next is None:
65-
return True
63+
def isPalindrome(self, head: Optional[ListNode]) -> bool:
6664
slow, fast = head, head.next
6765
while fast and fast.next:
6866
slow, fast = slow.next, fast.next.next

solution/0200-0299/0235.Lowest Common Ancestor of a Binary Search Tree/README.md

Lines changed: 131 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,15 @@
4242

4343
<!-- 这里可写通用的实现逻辑 -->
4444

45-
从上到下搜索,找到第一个值位于 `[p, q]` 之间的结点即可。既可以用迭代实现,也可以用递归实现。
45+
**方法一:迭代或递归**
46+
47+
从上到下搜索,找到第一个值位于 $[p.val, q.val]$ 之间的结点即可。
48+
49+
既可以用迭代实现,也可以用递归实现。
50+
51+
迭代的时间复杂度为 $O(n)$,空间复杂度为 $O(1)$。
52+
53+
递归的时间复杂度为 $O(n)$,空间复杂度为 $O(n)$。
4654

4755
<!-- tabs:start -->
4856

@@ -65,7 +73,7 @@ class Solution:
6573
def lowestCommonAncestor(
6674
self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode'
6775
) -> 'TreeNode':
68-
while root:
76+
while 1:
6977
if root.val < min(p.val, q.val):
7078
root = root.right
7179
elif root.val > max(p.val, q.val):
@@ -84,10 +92,11 @@ class Solution:
8492
# self.left = None
8593
# self.right = None
8694

95+
8796
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':
91100
if root.val < min(p.val, q.val):
92101
return self.lowestCommonAncestor(root.right, p, q)
93102
if root.val > max(p.val, q.val):
@@ -114,12 +123,15 @@ class Solution:
114123

115124
class Solution {
116125
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+
}
121134
}
122-
return root;
123135
}
124136
}
125137
```
@@ -139,78 +151,73 @@ class Solution {
139151

140152
class Solution {
141153
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+
}
145160
return root;
146161
}
147162
}
148163
```
149164

150-
### **TypeScript**
165+
### **C++**
151166

152167
迭代:
153168

154-
```ts
169+
```cpp
155170
/**
156171
* 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+
* };
167178
*/
168179

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+
}
181191
}
182192
}
183-
}
193+
};
184194
```
185195
186196
递归:
187197
188-
```ts
198+
```cpp
189199
/**
190200
* 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+
* };
201207
*/
202208
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+
};
214221
```
215222

216223
### **Go**
@@ -228,16 +235,15 @@ function lowestCommonAncestor(
228235
*/
229236

230237
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 {
235240
root = root.Right
241+
} else if root.Val > p.Val && root.Val > q.Val {
242+
root = root.Left
236243
} else {
237244
return root
238245
}
239246
}
240-
return nil
241247
}
242248
```
243249

@@ -254,72 +260,86 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
254260
*/
255261

256262
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
267270
}
268271
```
269272

270-
### **C++**
273+
### **TypeScript**
271274

272275
迭代:
273276

274-
```cpp
277+
```ts
275278
/**
276279
* 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+
* }
283290
*/
284291

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;
295304
}
296-
return root;
297305
}
298-
};
306+
}
299307
```
300308

301309
递归:
302310

303-
```cpp
311+
```ts
304312
/**
305313
* 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+
* }
312324
*/
313325

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+
323343
```
324344

325345
<!-- tabs:end -->

0 commit comments

Comments
 (0)