当前位置:网站首页>236. 二叉树的最近公共祖先
236. 二叉树的最近公共祖先
2022-07-26 16:55:00 【爱吃糖的徐菜菜】
文章目录
236. 二叉树的最近公共祖先
给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个节点 p、q,最近公共祖先表示为一个节点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
示例 1:

输入:root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
输出:3
解释:节点 5 和节点 1 的最近公共祖先是节点 3 。
示例 2:

输入:root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
输出:5
解释:节点 5 和节点 4 的最近公共祖先是节点 5 。因为根据定义最近公共祖先节点可以为节点本身。
示例 3:
输入:root = [1,2], p = 1, q = 2
输出:1
提示:
- 树中节点数目在范围
[2, 105]内。 -109 <= Node.val <= 109- 所有
Node.val互不相同。 p != qp和q均存在于给定的二叉树中。
思路:
函数lowestCommonAncestor的返回值是节点p和q的最近公共祖先,可以很容易想到,从下到上,依次检查每一个节点,是否是节点p和q的最近公共祖先.
需要满足条件:
- p和q在root的两侧
- 或者q或者p等于root,另外一个节点在root的子树里
深入理解函数lowestCommonAncestor的返回值:
- 如果树中没有p且没有q,就返回
NULL - 如果树中只有p或者只有q,就返回p或者q
- 有p且有q,就返回最近公共祖先
整理:
结合上面的思路,可以想到,使用递归,直到找到p和q,然后在递归的回溯过程中,求解问题.参考代码随想录的思路:
- 确定递归函数返回值以及参数
返回值的含义是:节点p和q的最近公共祖先
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q)
确定终止条件
如果树中没有p且没有q,就返回
NULL如果树中只有p或者只有q,就返回p或者q
if (!root) return NULL; if (root == q || root == p) return root;
- 递归逻辑
- 如果left和right都有非空返回值,说明root是最近公共祖先
- 如果有一个为空,则非空的那个是以root为根节点的树的
lowestCommonAncestor函数返回值 - 如果都为空,则NULL是以root为根节点的树的
lowestCommonAncestor函数返回值
TreeNode* left = lowestCommonAncestor(root->left, p, q);
TreeNode* right = lowestCommonAncestor(root->right, p, q);
代码:
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (!root) return NULL;
if (root == q || root == p) return root;
TreeNode* left = lowestCommonAncestor(root->left, p, q);
TreeNode* right = lowestCommonAncestor(root->right, p, q);
if (left && right)
return root;
else if (!left)
return right;
else
return left;
}
};
总结:
这个虽然是个简单题,但是我理解了很久很久,写篇题解做一下笔记.
边栏推荐
- 图解用户登录验证流程,写得太好了!
- 股票公司开户万一免五这是真的安全靠谱的吗
- Heavy! Zeng Xuezhong was promoted to vice chairman and CEO of zhanrui, and Chu Qingren was appointed as co CEO!
- hosts该文件已设置为只读的解决方法
- Tupu 3D visual national style design | collision between technology and culture "cool" spark“
- JS function scope variables declare that the variables that promote the scope chain without VaR are global variables
- Hardware development and market industry
- [cloud native kubernetes actual combat] kubeopertor installation tutorial
- JS 递归 斐波那契数列 深克隆
- ACL实验演示(Huawei路由器设备配置)
猜你喜欢

JS 递归 斐波那契数列 深克隆

机器视觉在服务机器人中的应用

ACL实验演示(Huawei路由器设备配置)

In the first half of the year, sales increased by 10% against the trend. You can always trust Volvo, which is persistent and safe

kudu设计-tablet

The principle of reliable transmission in TCP protocol
2.1.2 synchronization always fails

Week 16 OJ practice 1 calculates the day of the year

硬件开发与市场产业

#夏日挑战赛# OpenHarmony基于JS实现的贪吃蛇
随机推荐
Summer Challenge openharmony greedy snake based on JS
TD database syntax
【云原生之kubernetes实战】安装kubeopertor教程
如何使用 align-regexp 对齐 userscript 元信息
Definition of graph traversal and depth first search and breadth first search (I)
The first self-developed embedded 40nm industrial scale memory chip in China was released, breaking the status quo that the localization rate is zero
Pytorch中的tensor操作
Tupu 3D visual national style design | collision between technology and culture "cool" spark“
How to write plug-ins quickly with elisp
6-19漏洞利用-nsf获取目标密码文件
In the first half of the year, sales increased by 10% against the trend. You can always trust Volvo, which is persistent and safe
Gan (generative adversarial network, GaN) generative countermeasure network
Brief introduction to CUDA image construction
ASEMI整流桥KBPC3510,KBPC3510封装,KBPC3510应用
JS closure simulates private variable interview questions and immediately executes function Iife
In May, 2022, video user insight: user use time increased, and the platform achieved initial results in cost reduction and efficiency increase
The user experience center of Analysys Qianfan bank was established to help upgrade the user experience of the banking industry
After Australia, New Zealand announced the ban on Huawei 5g! Huawei official response
SQL中去去重的三种方式
the loss outweighs the gain! Doctors cheated 2.1 million yuan and masters cheated 30000 yuan of talent subsidies, all of which were sentenced!