Skip to content

Commit dfacd96

Browse files
committed
Merge branch 'master' of github.com:yanglbme/leetcode
2 parents 7b7d570 + f0b41b4 commit dfacd96

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Complete solutions to Leetcode problems, updated daily.
3030
| 198 | [House Robber](https://github.com/yanglbme/leetcode/tree/master/solution/198.House%20Robber) | `Dynamic Programming` |
3131
| 203 | [Remove Linked List Elements](https://github.com/yanglbme/leetcode/tree/master/solution/203.Remove%20Linked%20List%20Elements) | `Linked List` |
3232
| 231 | [Power of Two](https://github.com/yanglbme/leetcode/tree/master/solution/231.Power%20of%20Two) | `Math`, `Bit Manipulation` |
33+
| 235 | [Lowest Common Ancestor of a Binary Search Tree](https://github.com/yanglbme/leetcode/tree/master/solution/235.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree) | `Tree` |
3334
| 237 | [Delete Node in a Linked List](https://github.com/yanglbme/leetcode/tree/master/solution/237.Delete%20Node%20in%20a%20Linked%20List) | `Linked List` |
3435
| 344 | [Reverse String](https://github.com/yanglbme/leetcode/tree/master/solution/344.Reverse%20String) | `Two Pointers`, `String` |
3536
| 876 | [Middle of the Linked List](https://github.com/yanglbme/leetcode/tree/master/solution/876.Middle%20of%20the%20Linked%20List) | `Linked List` |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
## 二叉搜索树的最近公共祖先
2+
### 题目描述
3+
4+
给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。
5+
6+
[百度百科](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 的深度尽可能大(**一个节点也可以是它自己的祖先**)。”
7+
8+
例如,给定如下二叉搜索树: root = [6,2,8,0,4,7,9,null,null,3,5]
9+
```
10+
_______6______
11+
/ \
12+
___2__ ___8__
13+
/ \ / \
14+
0 _4 7 9
15+
/ \
16+
3 5
17+
```
18+
19+
示例 1:
20+
```
21+
输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
22+
输出: 6
23+
解释: 节点 2 和节点 8 的最近公共祖先是 6。
24+
```
25+
26+
示例 2:
27+
```
28+
输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
29+
输出: 2
30+
解释: 节点 2 和节点 4 的最近公共祖先是 2, 因为根据定义最近公共祖先节点可以为节点本身。
31+
```
32+
33+
说明:
34+
35+
- 所有节点的值都是唯一的。
36+
- p、q 为不同节点且均存在于给定的二叉搜索树中。
37+
38+
### 解法
39+
如果 root 左子树存在 p,右子树存在 q,那么 root 为最近祖先;
40+
如果 p、q 均在 root 左子树,递归左子树;右子树同理。
41+
42+
43+
```java
44+
/**
45+
* Definition for a binary tree node.
46+
* public class TreeNode {
47+
* int val;
48+
* TreeNode left;
49+
* TreeNode right;
50+
* TreeNode(int x) { val = x; }
51+
* }
52+
*/
53+
class Solution {
54+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
55+
if (root == null || root == p || root == q) {
56+
return root;
57+
}
58+
TreeNode leftNode = lowestCommonAncestor(root.left, p, q);
59+
TreeNode rightNode = lowestCommonAncestor(root.right, p, q);
60+
if (leftNode != null && rightNode != null) {
61+
return root;
62+
}
63+
return leftNode != null ? leftNode : (rightNode != null) ? rightNode : null;
64+
65+
}
66+
}
67+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
3+
if (root == null || root == p || root == q) {
4+
return root;
5+
}
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;
10+
}
11+
return leftNode != null ? leftNode : (rightNode != null) ? rightNode : null;
12+
13+
}
14+
}

0 commit comments

Comments
 (0)