mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Update
This commit is contained in:
@ -7,7 +7,7 @@
|
||||
<p align="center"><strong>欢迎大家<a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
|
||||
|
||||
|
||||
## 235. 二叉搜索树的最近公共祖先
|
||||
# 235. 二叉搜索树的最近公共祖先
|
||||
|
||||
[力扣题目链接](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/)
|
||||
|
||||
@ -21,14 +21,15 @@
|
||||
|
||||
示例 1:
|
||||
|
||||
输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
|
||||
输出: 6
|
||||
解释: 节点 2 和节点 8 的最近公共祖先是 6。
|
||||
* 输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
|
||||
* 输出: 6
|
||||
* 解释: 节点 2 和节点 8 的最近公共祖先是 6。
|
||||
|
||||
示例 2:
|
||||
|
||||
输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
|
||||
输出: 2
|
||||
解释: 节点 2 和节点 4 的最近公共祖先是 2, 因为根据定义最近公共祖先节点可以为节点本身。
|
||||
* 输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
|
||||
* 输出: 2
|
||||
* 解释: 节点 2 和节点 4 的最近公共祖先是 2, 因为根据定义最近公共祖先节点可以为节点本身。
|
||||
|
||||
|
||||
说明:
|
||||
@ -36,7 +37,9 @@
|
||||
* 所有节点的值都是唯一的。
|
||||
* p、q 为不同节点且均存在于给定的二叉搜索树中。
|
||||
|
||||
## 思路
|
||||
# 思路
|
||||
|
||||
|
||||
|
||||
做过[二叉树:公共祖先问题](https://programmercarl.com/0236.二叉树的最近公共祖先.html)题目的同学应该知道,利用回溯从底向上搜索,遇到一个节点的左子树里有p,右子树里有q,那么当前节点就是最近公共祖先。
|
||||
|
||||
@ -58,6 +61,7 @@
|
||||
|
||||
可以看出直接按照指定的方向,就可以找到节点4,为最近公共祖先,而且不需要遍历整棵树,找到结果直接返回!
|
||||
|
||||
## 递归法
|
||||
|
||||
递归三部曲如下:
|
||||
|
||||
@ -111,7 +115,6 @@ if (cur->val > p->val && cur->val > q->val) {
|
||||
|
||||
```
|
||||
if (递归函数(root->left)) return ;
|
||||
|
||||
if (递归函数(root->right)) return ;
|
||||
```
|
||||
|
||||
@ -128,7 +131,7 @@ left与right的逻辑处理;
|
||||
|
||||
如果 cur->val 小于 p->val,同时 cur->val 小于 q->val,那么就应该向右遍历(目标区间在右子树)。
|
||||
|
||||
```
|
||||
```CPP
|
||||
if (cur->val < p->val && cur->val < q->val) {
|
||||
TreeNode* right = traversal(cur->right, p, q);
|
||||
if (right != NULL) {
|
||||
@ -140,9 +143,9 @@ if (cur->val < p->val && cur->val < q->val) {
|
||||
剩下的情况,就是cur节点在区间(p->val <= cur->val && cur->val <= q->val)或者 (q->val <= cur->val && cur->val <= p->val)中,那么cur就是最近公共祖先了,直接返回cur。
|
||||
|
||||
代码如下:
|
||||
|
||||
```
|
||||
return cur;
|
||||
|
||||
```
|
||||
|
||||
那么整体递归代码如下:
|
||||
@ -216,7 +219,7 @@ public:
|
||||
|
||||
灵魂拷问:是不是又被简单的迭代法感动到痛哭流涕?
|
||||
|
||||
## 总结
|
||||
# 总结
|
||||
|
||||
对于二叉搜索树的最近祖先问题,其实要比[普通二叉树公共祖先问题](https://programmercarl.com/0236.二叉树的最近公共祖先.html)简单的多。
|
||||
|
||||
@ -225,10 +228,15 @@ public:
|
||||
最后给出了对应的迭代法,二叉搜索树的迭代法甚至比递归更容易理解,也是因为其有序性(自带方向性),按照目标区间找就行了。
|
||||
|
||||
|
||||
## 其他语言版本
|
||||
# 其他语言版本
|
||||
|
||||
|
||||
Java:
|
||||
## Java
|
||||
|
||||
递归法:
|
||||
|
||||
|
||||
迭代法:
|
||||
```java
|
||||
class Solution {
|
||||
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
|
||||
@ -246,15 +254,11 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
```python
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, x):
|
||||
# self.val = x
|
||||
# self.left = None
|
||||
# self.right = None
|
||||
|
||||
## Python
|
||||
|
||||
递归法:
|
||||
```python
|
||||
class Solution:
|
||||
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
|
||||
if not root: return root //中
|
||||
@ -264,18 +268,14 @@ class Solution:
|
||||
return self.lowestCommonAncestor(root.right,p,q) //右
|
||||
else: return root
|
||||
```
|
||||
Go:
|
||||
> BSL法
|
||||
|
||||
迭代法:
|
||||
|
||||
|
||||
## Go
|
||||
|
||||
递归法:
|
||||
```go
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
* Val int
|
||||
* Left *TreeNode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
//利用BSL的性质(前序遍历有序)
|
||||
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
|
||||
if root==nil{return nil}
|
||||
@ -287,34 +287,10 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
|
||||
}
|
||||
```
|
||||
|
||||
> 普通法
|
||||
|
||||
```go
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
* Val int
|
||||
* Left *TreeNode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
//递归会将值层层返回
|
||||
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
|
||||
//终止条件
|
||||
if root==nil||root.Val==p.Val||root.Val==q.Val{return root}//最后为空或者找到一个值时,就返回这个值
|
||||
//后序遍历
|
||||
findLeft:=lowestCommonAncestor(root.Left,p,q)
|
||||
findRight:=lowestCommonAncestor(root.Right,p,q)
|
||||
//处理单层逻辑
|
||||
if findLeft!=nil&&findRight!=nil{return root}//说明在root节点的两边
|
||||
if findLeft==nil{//左边没找到,就说明在右边找到了
|
||||
return findRight
|
||||
}else {return findLeft}
|
||||
}
|
||||
```
|
||||
## JavaScript
|
||||
|
||||
JavaScript版本:
|
||||
1. 使用递归的方法
|
||||
递归法:
|
||||
```javascript
|
||||
var lowestCommonAncestor = function(root, p, q) {
|
||||
// 使用递归的方法
|
||||
@ -336,7 +312,8 @@ var lowestCommonAncestor = function(root, p, q) {
|
||||
return root;
|
||||
};
|
||||
```
|
||||
2. 使用迭代的方法
|
||||
|
||||
迭代法
|
||||
```javascript
|
||||
var lowestCommonAncestor = function(root, p, q) {
|
||||
// 使用迭代的方法
|
||||
@ -355,7 +332,6 @@ var lowestCommonAncestor = function(root, p, q) {
|
||||
```
|
||||
|
||||
|
||||
|
||||
-----------------------
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||
|
@ -137,7 +137,7 @@ dp[0][j] 和 dp[i][0] 都已经初始化了,那么其他下标应该初始化
|
||||
|
||||
```
|
||||
// 初始化 dp
|
||||
vector<vector<int>> dp(weight.size() + 1, vector<int>(bagWeight + 1, 0));
|
||||
vector<vector<int>> dp(weight.size(), vector<int>(bagWeight + 1, 0));
|
||||
for (int j = weight[0]; j <= bagWeight; j++) {
|
||||
dp[0][j] = value[0];
|
||||
}
|
||||
|
Reference in New Issue
Block a user