Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ebd88a6

Browse files
feat: add solutions to lc problem: No.0235 (doocs#4265)
1 parent bc5df23 commit ebd88a6

File tree

6 files changed

+200
-24
lines changed

6 files changed

+200
-24
lines changed

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

Lines changed: 73 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ tags:
3232
<p><strong>示例 1:</strong></p>
3333

3434
<pre><strong>输入:</strong> root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
35-
<strong>输出:</strong> 6
35+
<strong>输出:</strong> 6
3636
<strong>解释: </strong>节点 <code>2 </code>和节点 <code>8 </code>的最近公共祖先是 <code>6。</code>
3737
</pre>
3838

@@ -57,15 +57,11 @@ tags:
5757

5858
<!-- solution:start -->
5959

60-
### 方法一:迭代或递归
60+
### 方法一:迭代
6161

62-
从上到下搜索,找到第一个值位于 $[p.val, q.val]$ 之间的结点即可
62+
我们从根节点开始遍历,如果当前节点的值小于 $\textit{p}$ 和 $\textit{q}$ 的值,说明 $\textit{p}$ 和 $\textit{q}$ 应该在当前节点的右子树,因此将当前节点移动到右子节点;如果当前节点的值大于 $\textit{p}$ 和 $\textit{q}$ 的值,说明 $\textit{p}$ 和 $\textit{q}$ 应该在当前节点的左子树,因此将当前节点移动到左子节点;否则说明当前节点就是 $\textit{p}$ 和 $\textit{q}$ 的最近公共祖先,返回当前节点即可
6363

64-
既可以用迭代实现,也可以用递归实现。
65-
66-
迭代的时间复杂度为 $O(n),ドル空间复杂度为 $O(1)$。
67-
68-
递归的时间复杂度为 $O(n),ドル空间复杂度为 $O(n)$。
64+
时间复杂度 $O(n),ドル其中 $n$ 是二叉搜索树的节点个数。空间复杂度 $O(1)$。
6965

7066
<!-- tabs:start -->
7167

@@ -164,9 +160,9 @@ public:
164160
165161
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
166162
for {
167-
if root.Val < p.Val && root.Val < q.Val {
163+
if root.Val < min(p.Val, q.Val) {
168164
root = root.Right
169-
} else if root.Val > p.Val && root.Val > q.Val {
165+
} else if root.Val > max(p.Val, q.Val) {
170166
root = root.Left
171167
} else {
172168
return root
@@ -209,13 +205,47 @@ function lowestCommonAncestor(
209205
}
210206
```
211207

208+
#### C#
209+
210+
```cs
211+
/**
212+
* Definition for a binary tree node.
213+
* public class TreeNode {
214+
* public int val;
215+
* public TreeNode left;
216+
* public TreeNode right;
217+
* public TreeNode(int x) { val = x; }
218+
* }
219+
*/
220+
221+
public class Solution {
222+
public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
223+
while (true) {
224+
if (root.val < Math.Min(p.val, q.val)) {
225+
root = root.right;
226+
} else if (root.val > Math.Max(p.val, q.val)) {
227+
root = root.left;
228+
} else {
229+
return root;
230+
}
231+
}
232+
}
233+
}
234+
```
235+
212236
<!-- tabs:end -->
213237

214238
<!-- solution:end -->
215239

216240
<!-- solution:start -->
217241

218-
### 方法二
242+
### 方法二:递归
243+
244+
我们也可以使用递归的方法来解决这个问题。
245+
246+
我们首先判断当前节点的值是否小于 $\textit{p}$ 和 $\textit{q}$ 的值,如果是,则递归遍历右子树;如果当前节点的值大于 $\textit{p}$ 和 $\textit{q}$ 的值,如果是,则递归遍历左子树;否则说明当前节点就是 $\textit{p}$ 和 $\textit{q}$ 的最近公共祖先,返回当前节点即可。
247+
248+
时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 是二叉搜索树的节点个数。
219249

220250
<!-- tabs:start -->
221251

@@ -339,12 +369,42 @@ function lowestCommonAncestor(
339369
p: TreeNode | null,
340370
q: TreeNode | null,
341371
): TreeNode | null {
342-
if (root.val > p.val && root.val > q.val) return lowestCommonAncestor(root.left, p, q);
343-
if (root.val < p.val && root.val < q.val) return lowestCommonAncestor(root.right, p, q);
372+
if (root.val > p.val && root.val > q.val) {
373+
return lowestCommonAncestor(root.left, p, q);
374+
}
375+
if (root.val < p.val && root.val < q.val) {
376+
return lowestCommonAncestor(root.right, p, q);
377+
}
344378
return root;
345379
}
346380
```
347381

382+
#### C#
383+
384+
```cs
385+
/**
386+
* Definition for a binary tree node.
387+
* public class TreeNode {
388+
* public int val;
389+
* public TreeNode left;
390+
* public TreeNode right;
391+
* public TreeNode(int x) { val = x; }
392+
* }
393+
*/
394+
395+
public class Solution {
396+
public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
397+
if (root.val < Math.Min(p.val, q.val)) {
398+
return LowestCommonAncestor(root.right, p, q);
399+
}
400+
if (root.val > Math.Max(p.val, q.val)) {
401+
return LowestCommonAncestor(root.left, p, q);
402+
}
403+
return root;
404+
}
405+
}
406+
```
407+
348408
<!-- tabs:end -->
349409

350410
<!-- solution:end -->

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

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ tags:
6464

6565
<!-- solution:start -->
6666

67-
### Solution 1
67+
### Solution 1: Iteration
68+
69+
Starting from the root node, we traverse the tree. If the current node's value is less than both $\textit{p}$ and $\textit{q}$ values, it means that $\textit{p}$ and $\textit{q}$ should be in the right subtree of the current node, so we move to the right child. If the current node's value is greater than both $\textit{p}$ and $\textit{q}$ values, it means that $\textit{p}$ and $\textit{q}$ should be in the left subtree, so we move to the left child. Otherwise, it means the current node is the lowest common ancestor of $\textit{p}$ and $\textit{q},ドル so we return the current node.
70+
71+
The time complexity is $O(n),ドル where $n$ is the number of nodes in the binary search tree. The space complexity is $O(1)$.
6872

6973
<!-- tabs:start -->
7074

@@ -163,9 +167,9 @@ public:
163167
164168
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
165169
for {
166-
if root.Val < p.Val && root.Val < q.Val {
170+
if root.Val < min(p.Val, q.Val) {
167171
root = root.Right
168-
} else if root.Val > p.Val && root.Val > q.Val {
172+
} else if root.Val > max(p.Val, q.Val) {
169173
root = root.Left
170174
} else {
171175
return root
@@ -208,13 +212,47 @@ function lowestCommonAncestor(
208212
}
209213
```
210214

215+
#### C#
216+
217+
```cs
218+
/**
219+
* Definition for a binary tree node.
220+
* public class TreeNode {
221+
* public int val;
222+
* public TreeNode left;
223+
* public TreeNode right;
224+
* public TreeNode(int x) { val = x; }
225+
* }
226+
*/
227+
228+
public class Solution {
229+
public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
230+
while (true) {
231+
if (root.val < Math.Min(p.val, q.val)) {
232+
root = root.right;
233+
} else if (root.val > Math.Max(p.val, q.val)) {
234+
root = root.left;
235+
} else {
236+
return root;
237+
}
238+
}
239+
}
240+
}
241+
```
242+
211243
<!-- tabs:end -->
212244

213245
<!-- solution:end -->
214246

215247
<!-- solution:start -->
216248

217-
### Solution 2
249+
### Solution 2: Recursion
250+
251+
We can also use a recursive approach to solve this problem.
252+
253+
We first check if the current node's value is less than both $\textit{p}$ and $\textit{q}$ values. If it is, we recursively traverse the right subtree. If the current node's value is greater than both $\textit{p}$ and $\textit{q}$ values, we recursively traverse the left subtree. Otherwise, it means the current node is the lowest common ancestor of $\textit{p}$ and $\textit{q},ドル so we return the current node.
254+
255+
The time complexity is $O(n),ドル and the space complexity is $O(n)$. Where $n$ is the number of nodes in the binary search tree.
218256

219257
<!-- tabs:start -->
220258

@@ -338,12 +376,42 @@ function lowestCommonAncestor(
338376
p: TreeNode | null,
339377
q: TreeNode | null,
340378
): TreeNode | null {
341-
if (root.val > p.val && root.val > q.val) return lowestCommonAncestor(root.left, p, q);
342-
if (root.val < p.val && root.val < q.val) return lowestCommonAncestor(root.right, p, q);
379+
if (root.val > p.val && root.val > q.val) {
380+
return lowestCommonAncestor(root.left, p, q);
381+
}
382+
if (root.val < p.val && root.val < q.val) {
383+
return lowestCommonAncestor(root.right, p, q);
384+
}
343385
return root;
344386
}
345387
```
346388

389+
#### C#
390+
391+
```cs
392+
/**
393+
* Definition for a binary tree node.
394+
* public class TreeNode {
395+
* public int val;
396+
* public TreeNode left;
397+
* public TreeNode right;
398+
* public TreeNode(int x) { val = x; }
399+
* }
400+
*/
401+
402+
public class Solution {
403+
public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
404+
if (root.val < Math.Min(p.val, q.val)) {
405+
return LowestCommonAncestor(root.right, p, q);
406+
}
407+
if (root.val > Math.Max(p.val, q.val)) {
408+
return LowestCommonAncestor(root.left, p, q);
409+
}
410+
return root;
411+
}
412+
}
413+
```
414+
347415
<!-- tabs:end -->
348416

349417
<!-- solution:end -->
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* public int val;
5+
* public TreeNode left;
6+
* public TreeNode right;
7+
* public TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
11+
public class Solution {
12+
public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
13+
while (true) {
14+
if (root.val < Math.Min(p.val, q.val)) {
15+
root = root.right;
16+
} else if (root.val > Math.Max(p.val, q.val)) {
17+
root = root.left;
18+
} else {
19+
return root;
20+
}
21+
}
22+
}
23+
}

‎solution/0200-0299/0235.Lowest Common Ancestor of a Binary Search Tree/Solution.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
1111
for {
12-
if root.Val < p.Val&&root.Val<q.Val {
12+
if root.Val < min(p.Val, q.Val) {
1313
root = root.Right
14-
} else if root.Val > p.Val&&root.Val>q.Val {
14+
} else if root.Val > max(p.Val, q.Val) {
1515
root = root.Left
1616
} else {
1717
return root
1818
}
1919
}
20-
}
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* public int val;
5+
* public TreeNode left;
6+
* public TreeNode right;
7+
* public TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
11+
public class Solution {
12+
public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
13+
if (root.val < Math.Min(p.val, q.val)) {
14+
return LowestCommonAncestor(root.right, p, q);
15+
}
16+
if (root.val > Math.Max(p.val, q.val)) {
17+
return LowestCommonAncestor(root.left, p, q);
18+
}
19+
return root;
20+
}
21+
}

‎solution/0200-0299/0235.Lowest Common Ancestor of a Binary Search Tree/Solution2.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ function lowestCommonAncestor(
1717
p: TreeNode | null,
1818
q: TreeNode | null,
1919
): TreeNode | null {
20-
if (root.val > p.val && root.val > q.val) return lowestCommonAncestor(root.left, p, q);
21-
if (root.val < p.val && root.val < q.val) return lowestCommonAncestor(root.right, p, q);
20+
if (root.val > p.val && root.val > q.val) {
21+
return lowestCommonAncestor(root.left, p, q);
22+
}
23+
if (root.val < p.val && root.val < q.val) {
24+
return lowestCommonAncestor(root.right, p, q);
25+
}
2226
return root;
2327
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /