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 ef96e9c

Browse files
committed
feat: update leetcode solutions: No.0235, No.0236
1 parent 977b537 commit ef96e9c

File tree

12 files changed

+251
-40
lines changed

12 files changed

+251
-40
lines changed

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949

5050
### 二叉树
5151

52-
1. [二叉树的最近公共祖先](./lcof/面试题68%20-%20II.%20二叉树的最近公共祖先/README.md)
53-
1. [二叉搜索树的最近公共祖先](./lcof/面试题68%20-%20I.%20二叉搜索树的最近公共祖先/README.md)
52+
1. [二叉树的最近公共祖先](./solution/0200-0299/0235.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree/README.md)
53+
1. [二叉搜索树的最近公共祖先](./solution/0200-0299/0236.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree/README.md)
5454

5555
### 数学
5656

‎README_EN.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
4747

4848
### Binary Tree
4949

50+
1. [Lowest Common Ancestor of a Binary Tree](./solution/0200-0299/0236.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree/README_EN.md)
51+
1. [Lowest Common Ancestor of a Binary Search Tree](./solution/0200-0299/0235.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree/README_EN.md)
52+
53+
5054
### Math
5155

5256
1. [Set Mismatch](./solution/0600-0699/0645.Set%20Mismatch/README_EN.md)

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

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111
<p>例如,给定如下二叉搜索树:&nbsp; root =&nbsp;[6,2,8,0,4,7,9,null,null,3,5]</p>
1212

13-
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/14/binarysearchtree_improved.png" style="height: 190px; width: 200px;"></p>
14-
15-
<p>&nbsp;</p>
13+
![](./images/binarysearchtree_improved.png)
1614

1715
<p><strong>示例 1:</strong></p>
1816

@@ -41,21 +39,65 @@
4139
## 解法
4240
<!-- 这里可写通用的实现逻辑 -->
4341

42+
从上到下搜索,找到第一个值位于 `[p, q]` 之间的结点即可。既可以用迭代实现,也可以用递归实现。
4443

4544
<!-- tabs:start -->
4645

4746
### **Python3**
4847
<!-- 这里可写当前语言的特殊实现逻辑 -->
4948

5049
```python
51-
50+
# Definition for a binary tree node.
51+
# class TreeNode:
52+
# def __init__(self, x):
53+
# self.val = x
54+
# self.left = None
55+
# self.right = None
56+
57+
class Solution:
58+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
59+
if p == q:
60+
return p
61+
while root:
62+
if root.val < p.val and root.val < q.val:
63+
root = root.right
64+
elif root.val > p.val and root.val > q.val:
65+
root = root.left
66+
else:
67+
return root
5268
```
5369

5470
### **Java**
5571
<!-- 这里可写当前语言的特殊实现逻辑 -->
5672

5773
```java
58-
74+
/**
75+
* Definition for a binary tree node.
76+
* public class TreeNode {
77+
* int val;
78+
* TreeNode left;
79+
* TreeNode right;
80+
* TreeNode(int x) { val = x; }
81+
* }
82+
*/
83+
84+
class Solution {
85+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
86+
if (p == q) {
87+
return p;
88+
}
89+
while (root != null) {
90+
if (root.val < p.val && root.val < q.val) {
91+
root = root.right;
92+
} else if (root.val > p.val && root.val > q.val) {
93+
root = root.left;
94+
} else {
95+
return root;
96+
}
97+
}
98+
return null;
99+
}
100+
}
59101
```
60102

61103
### **...**

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

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@
1313

1414
<p>Given binary search tree:&nbsp; root =&nbsp;[6,2,8,0,4,7,9,null,null,3,5]</p>
1515

16-
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarysearchtree_improved.png" style="width: 200px; height: 190px;" />
17-
18-
<p>&nbsp;</p>
19-
20-
16+
![](./images/binarysearchtree_improved.png)
2117

2218
<p><strong>Example 1:</strong></p>
2319

@@ -60,11 +56,8 @@
6056

6157

6258
<ul>
63-
6459
<li>All of the nodes&#39; values will be unique.</li>
65-
6660
<li>p and q are different and both values will&nbsp;exist in the BST.</li>
67-
6861
</ul>
6962

7063

@@ -78,13 +71,56 @@
7871
### **Python3**
7972

8073
```python
81-
74+
# Definition for a binary tree node.
75+
# class TreeNode:
76+
# def __init__(self, x):
77+
# self.val = x
78+
# self.left = None
79+
# self.right = None
80+
81+
class Solution:
82+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
83+
if p == q:
84+
return p
85+
while root:
86+
if root.val < p.val and root.val < q.val:
87+
root = root.right
88+
elif root.val > p.val and root.val > q.val:
89+
root = root.left
90+
else:
91+
return root
8292
```
8393

8494
### **Java**
8595

8696
```java
87-
97+
/**
98+
* Definition for a binary tree node.
99+
* public class TreeNode {
100+
* int val;
101+
* TreeNode left;
102+
* TreeNode right;
103+
* TreeNode(int x) { val = x; }
104+
* }
105+
*/
106+
107+
class Solution {
108+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
109+
if (p == q) {
110+
return p;
111+
}
112+
while (root != null) {
113+
if (root.val < p.val && root.val < q.val) {
114+
root = root.right;
115+
} else if (root.val > p.val && root.val > q.val) {
116+
root = root.left;
117+
} else {
118+
return root;
119+
}
120+
}
121+
return null;
122+
}
123+
}
88124
```
89125

90126
### **...**
Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
111
class Solution {
212
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
3-
if (root == null || root == p || root == q) {
4-
return root;
13+
if (p == q) {
14+
return p;
515
}
6-
TreeNode leftNode = lowestCommonAncestor(root.left, p, q);
7-
TreeNode rightNode = lowestCommonAncestor(root.right, p, q);
8-
if (leftNode != null && rightNode != null) {
9-
return root;
16+
while (root != null) {
17+
if (root.val < p.val && root.val < q.val) {
18+
root = root.right;
19+
} else if (root.val > p.val && root.val > q.val) {
20+
root = root.left;
21+
} else {
22+
return root;
23+
}
1024
}
11-
return leftNode != null ? leftNode : (rightNode != null) ? rightNode : null;
12-
25+
return null;
1326
}
1427
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution:
9+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
10+
if p == q:
11+
return p
12+
while root:
13+
if root.val < p.val and root.val < q.val:
14+
root = root.right
15+
elif root.val > p.val and root.val > q.val:
16+
root = root.left
17+
else:
18+
return root
7.48 KB
Loading[フレーム]

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

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111
<p>例如,给定如下二叉树:&nbsp; root =&nbsp;[3,5,1,6,2,0,8,null,null,7,4]</p>
1212

13-
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/15/binarytree.png" style="height: 190px; width: 200px;"></p>
14-
15-
<p>&nbsp;</p>
13+
![](./images/binarytree.png)
1614

1715
<p><strong>示例 1:</strong></p>
1816

@@ -42,21 +40,64 @@
4240
## 解法
4341
<!-- 这里可写通用的实现逻辑 -->
4442

43+
根据"**最近公共祖先**"的定义,若 root 是 p, q 的最近公共祖先 ,则只可能为以下情况之一:
44+
45+
- 如果 p 和 q 分别是 root 的左右节点,那么 root 就是我们要找的最近公共祖先;
46+
- 如果 p 和 q 都是 root 的左节点,那么返回 `lowestCommonAncestor(root.left, p, q)`;
47+
- 如果 p 和 q 都是 root 的右节点,那么返回 `lowestCommonAncestor(root.right, p, q)`
48+
49+
**边界条件讨论**:
50+
51+
- 如果 root 为 null,则说明我们已经找到最底了,返回 null 表示没找到;
52+
- 如果 root 与 p 相等或者与 q 相等,则返回 root;
53+
- 如果左子树没找到,递归函数返回 null,证明 p 和 q 同在 root 的右侧,那么最终的公共祖先就是右子树找到的结点;
54+
- 如果右子树没找到,递归函数返回 null,证明 p 和 q 同在 root 的左侧,那么最终的公共祖先就是左子树找到的结点。
4555

4656
<!-- tabs:start -->
4757

4858
### **Python3**
4959
<!-- 这里可写当前语言的特殊实现逻辑 -->
5060

5161
```python
52-
62+
# Definition for a binary tree node.
63+
# class TreeNode:
64+
# def __init__(self, x):
65+
# self.val = x
66+
# self.left = None
67+
# self.right = None
68+
69+
class Solution:
70+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
71+
if root is None or root == p or root == q:
72+
return root
73+
left = self.lowestCommonAncestor(root.left, p, q)
74+
right = self.lowestCommonAncestor(root.right, p, q)
75+
return right if left is None else (left if right is None else root)
5376
```
5477

5578
### **Java**
5679
<!-- 这里可写当前语言的特殊实现逻辑 -->
5780

5881
```java
59-
82+
/**
83+
* Definition for a binary tree node.
84+
* public class TreeNode {
85+
* int val;
86+
* TreeNode left;
87+
* TreeNode right;
88+
* TreeNode(int x) { val = x; }
89+
* }
90+
*/
91+
class Solution {
92+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
93+
if (root == null || root == p || root == q) {
94+
return root;
95+
}
96+
TreeNode left = lowestCommonAncestor(root.left, p, q);
97+
TreeNode right = lowestCommonAncestor(root.right, p, q);
98+
return left == null ? right : (right == null ? left : root);
99+
}
100+
}
60101
```
61102

62103
### **...**

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

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@
1313

1414
<p>Given the following binary tree:&nbsp; root =&nbsp;[3,5,1,6,2,0,8,null,null,7,4]</p>
1515

16-
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
17-
18-
<p>&nbsp;</p>
19-
20-
16+
![](./images/binarytree.png)
2117

2218
<p><strong>Example 1:</strong></p>
2319

@@ -60,11 +56,8 @@
6056

6157

6258
<ul>
63-
6459
<li>All of the nodes&#39; values will be unique.</li>
65-
6660
<li>p and q are different and both values will&nbsp;exist in the binary tree.</li>
67-
6861
</ul>
6962

7063

@@ -78,13 +71,44 @@
7871
### **Python3**
7972

8073
```python
81-
74+
# Definition for a binary tree node.
75+
# class TreeNode:
76+
# def __init__(self, x):
77+
# self.val = x
78+
# self.left = None
79+
# self.right = None
80+
81+
class Solution:
82+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
83+
if root is None or root == p or root == q:
84+
return root
85+
left = self.lowestCommonAncestor(root.left, p, q)
86+
right = self.lowestCommonAncestor(root.right, p, q)
87+
return right if left is None else (left if right is None else root)
8288
```
8389

8490
### **Java**
8591

8692
```java
87-
93+
/**
94+
* Definition for a binary tree node.
95+
* public class TreeNode {
96+
* int val;
97+
* TreeNode left;
98+
* TreeNode right;
99+
* TreeNode(int x) { val = x; }
100+
* }
101+
*/
102+
class Solution {
103+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
104+
if (root == null || root == p || root == q) {
105+
return root;
106+
}
107+
TreeNode left = lowestCommonAncestor(root.left, p, q);
108+
TreeNode right = lowestCommonAncestor(root.right, p, q);
109+
return left == null ? right : (right == null ? left : root);
110+
}
111+
}
88112
```
89113

90114
### **...**

0 commit comments

Comments
(0)

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