当前位置:网站首页>LeetCode:236. 二叉树的最近公共祖先
LeetCode:236. 二叉树的最近公共祖先
2022-07-06 08:44:00 【Bertil】
给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 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, 10^5] 内。
- -10^9 <= Node.val <= 10^9
- 所有 Node.val 互不相同 。
- p != q
- p 和 q 均存在于给定的二叉树中。
解题思路
1.首先使用递归:root是null或者root等于p或q,说明root是p,q的公共祖先(递归结束),以此来递归左右子树
2.然后在左右子树递归完后进行判断
- 若左右子树递归函数返回的都不为空,则root就是p,q的公共祖先
- 若左子树递归函数返回的值为空,则p,q都在右子树,直接返回right即可
- 若右子树递归函数返回的值为空,则p,q都在左子树,直接返回left即可
代码
/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */
/** * @param {TreeNode} root * @param {TreeNode} p * @param {TreeNode} q * @return {TreeNode} */
var lowestCommonAncestor = function(root, p, q) {
const travelTree = function(root, p, q) {
// 递归终止条件
if(root === null || root === p|| root === q) {
return root;
}
let left = travelTree(root.left, p, q);
let right = travelTree(root.right, p, q);
//如果在某一个节点的左右子树都能找到p和q,说明这个节点就是公共祖先
if(left !== null && right !== null) {
return root;
}
if(left === null) {
return right;
}
return left;
}
return travelTree(root, p, q);
};
边栏推荐
- Current situation and trend of character animation
- swagger设置字段required必填
- Bottom up - physical layer
- PLT in Matplotlib tight_ layout()
- What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
- POI add write excel file
- JVM quick start
- 2022.02.13 - NC001. Reverse linked list
- Warning in install. packages : package ‘RGtk2’ is not available for this version of R
- Roguelike game into crack the hardest hit areas, how to break the bureau?
猜你喜欢
电脑清理,删除的系统文件
[embedded] print log using JLINK RTT
Sublime text using ctrl+b to run another program without closing other runs
Charging interface docking tutorial of enterprise and micro service provider platform
ESP8266-RTOS物联网开发
JVM performance tuning and practical basic theory - Part 1
PC easy to use essential software (used)
Roguelike game into crack the hardest hit areas, how to break the bureau?
TCP/IP协议
FairGuard游戏加固:游戏出海热潮下,游戏安全面临新挑战
随机推荐
Image,cv2读取图片的numpy数组的转换和尺寸resize变化
企微服务商平台收费接口对接教程
Esp8266-rtos IOT development
软件压力测试常见流程有哪些?专业出具软件测试报告公司分享
ROS编译 调用第三方动态库(xxx.so)
Promise 在uniapp的简单使用
Rviz仿真时遇到机器人瞬间回到世界坐标原点的问题及可能原因
China dihydrolaurenol market forecast and investment strategy report (2022 Edition)
C語言雙指針——經典題型
Unsupported operation exception
sublime text中conda环境中plt.show无法弹出显示图片的问题
Warning in install. packages : package ‘RGtk2’ is not available for this version of R
Sublime text using ctrl+b to run another program without closing other runs
[embedded] print log using JLINK RTT
torch建立的网络模型使用torchviz显示
Browser thread
marathon-envs项目环境配置(强化学习模仿参考动作)
sublime text没关闭其他运行就使用CTRL+b运行另外的程序问题
Using pkgbuild:: find in R language_ Rtools check whether rtools is available and use sys The which function checks whether make exists, installs it if not, and binds R and rtools with the writelines
The mysqlbinlog command uses