Skip to content

Commit c98e50b

Browse files
committed
feat: add python and java solutions to lcof question
添加《剑指 Offer》题解:面试题68 - I. 二叉搜索树的最近公共祖先
1 parent 08ced54 commit c98e50b

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# [面试题68 - I. 二叉搜索树的最近公共祖先](https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-zui-jin-gong-gong-zu-xian-lcof/)
2+
3+
## 题目描述
4+
<!-- 这里写题目描述 -->
5+
给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。
6+
7+
[百度百科](https://baike.baidu.com/item/%E6%9C%80%E8%BF%91%E5%85%AC%E5%85%B1%E7%A5%96%E5%85%88/8918834?fr=aladdin)中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(**一个节点也可以是它自己的祖先**)。”
8+
9+
例如,给定如下二叉搜索树:  root = `[6,2,8,0,4,7,9,null,null,3,5]`
10+
11+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/14/binarysearchtree_improved.png)
12+
13+
**示例 1:**
14+
15+
```
16+
输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
17+
输出: 6
18+
解释: 节点 2 和节点 8 的最近公共祖先是 6。
19+
```
20+
21+
**示例 2:**
22+
23+
```
24+
输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
25+
输出: 2
26+
解释: 节点 2 和节点 4 的最近公共祖先是 2, 因为根据定义最近公共祖先节点可以为节点本身。
27+
```
28+
29+
**说明:**
30+
31+
- 所有节点的值都是唯一的。
32+
- p、q 为不同节点且均存在于给定的二叉搜索树中。
33+
34+
## 解法
35+
<!-- 这里可写通用的实现逻辑 -->
36+
从上到下搜索,找到第一个值位于 `[p, q]` 之间的结点即可。
37+
38+
### Python3
39+
<!-- 这里可写当前语言的特殊实现逻辑 -->
40+
41+
```python
42+
# Definition for a binary tree node.
43+
# class TreeNode:
44+
# def __init__(self, x):
45+
# self.val = x
46+
# self.left = None
47+
# self.right = None
48+
49+
class Solution:
50+
def lowestCommonAncestor(self, root: TreeNode, p: TreeNode, q: TreeNode) -> TreeNode:
51+
if p == q:
52+
return p
53+
while root:
54+
if root.val < p.val and root.val < q.val:
55+
root = root.right
56+
elif root.val > p.val and root.val > q.val:
57+
root = root.left
58+
else:
59+
return root
60+
```
61+
62+
### Java
63+
<!-- 这里可写当前语言的特殊实现逻辑 -->
64+
65+
```java
66+
/**
67+
* Definition for a binary tree node.
68+
* public class TreeNode {
69+
* int val;
70+
* TreeNode left;
71+
* TreeNode right;
72+
* TreeNode(int x) { val = x; }
73+
* }
74+
*/
75+
class Solution {
76+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
77+
if (p == q) {
78+
return p;
79+
}
80+
while (root != null) {
81+
if (root.val < p.val && root.val < q.val) {
82+
root = root.right;
83+
} else if (root.val > p.val && root.val > q.val) {
84+
root = root.left;
85+
} else {
86+
return root;
87+
}
88+
}
89+
return null;
90+
}
91+
}
92+
```
93+
94+
### ...
95+
```
96+
97+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
class Solution {
11+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
12+
if (p == q) {
13+
return p;
14+
}
15+
while (root != null) {
16+
if (root.val < p.val && root.val < q.val) {
17+
root = root.right;
18+
} else if (root.val > p.val && root.val > q.val) {
19+
root = root.left;
20+
} else {
21+
return root;
22+
}
23+
}
24+
return null;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
9+
class Solution:
10+
def lowestCommonAncestor(self, root: TreeNode, p: TreeNode, q: TreeNode) -> TreeNode:
11+
if p == q:
12+
return p
13+
while root:
14+
if root.val < p.val and root.val < q.val:
15+
root = root.right
16+
elif root.val > p.val and root.val > q.val:
17+
root = root.left
18+
else:
19+
return root

0 commit comments

Comments
 (0)