当前位置:网站首页>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;
}
};
总结:
这个虽然是个简单题,但是我理解了很久很久,写篇题解做一下笔记.
边栏推荐
- 机器视觉在服务机器人中的应用
- JS 函数作用域 变量声明提升 作用域链 不加var的变量,是全局变量
- 2019普及组总结
- 二层管理型交换机如何设置IP
- 03 | implement usereducer and usestate
- RedisDesktopManager去除升级提示
- How to use different tools to analyze and optimize code performance when CPU utilization is high
- Stand aside with four and five rear cameras, LG or push the 16 rear camera mobile phone!
- The latest interface of Taobao / tmall keyword search
- How to use align regexp to align userscript meta information
猜你喜欢
2.1.2 synchronization always fails

Machine learning - what are machine learning, supervised learning, and unsupervised learning

RedisDesktopManager去除升级提示

Pass-19,20

Hardware development and market industry

(24)Blender源码分析之顶层菜单显示代码分析

浅析接口测试

SQL中去去重的三种方式

(25) top level menu of blender source code analysis blender menu

Definition of graph traversal and depth first search and breadth first search (I)
随机推荐
SQL injection (mind map)
Ascend target detection and recognition - customize your own AI application
我们被一个 kong 的性能 bug 折腾了一个通宵
如何快速使用 ELisp 进行插件编写
Avalanche subnets vs. polygon supernets of application chain
6-19 vulnerability exploitation -nsf to obtain the target password file
Redis hotspot key and big value
Heavy announcement! Icml2022 Awards: 15 outstanding papers, selected by Fudan University, Xiamen University and Shanghai Jiaotong University
Oracle is slow to perform a large number of DML operations. Is it the problem of CPU or hard disk?
性能调优bug层出不穷?这3份文档轻松搞定JVM调优
Detailed explanation of openwrt's feeds.conf.default
API analysis of Taobao / tmall shipping address list and express delivery fees
pip安装模块,报错
(24) the top menu of blender source code analysis shows code analysis
Tupu 3D visual national style design | collision between technology and culture "cool" spark“
Ascend目标检测与识别-定制自己的AI应用
Tree DP problem
Why are test / development programmers who are better paid than me? Abandoned by the times
A detailed explanation of throughput, QPS, TPS, concurrency and other high concurrency indicators
2022 年有哪些流行的技术?