当前位置:网站首页>剑指Offer 68 - II. 二叉树的最近公共祖先
剑指Offer 68 - II. 二叉树的最近公共祖先
2022-07-04 22:20:00 【LuZhouShiLi】
剑指 Offer 68 - II. 二叉树的最近公共祖先
题目
给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
思路
- 如果P和Q都是root的左右节点,那么root就是我们要找的最近公共祖先
- 如果P和Q都是root的左节点,那么返回lowestCommonAncestor(root.left,p,q)
- 如果P和Q都是root的右节点,那么返回lowestCommonAncestor(root.right,p,q)
边界条件讨论:
- 如果root = null 说明找不到,如果root = p或者root = q,返回root
- 如果左子树没找到,递归函数返回null,证明p和q都是在同测,那么就在右子树中查找
- 如果右子树没找到,递归函数返回null,证明p和q都是在同测,那么就在左子树中查找
代码
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null || root == p || root == q)
{
return root;
}
TreeNode leftNode = lowestCommonAncestor(root.left,p,q);
TreeNode rightNode = lowestCommonAncestor(root.right,p,q);
if(leftNode == null)
{
return rightNode;
}
if(rightNode == null)
{
return leftNode;
}
return root;
}
}
边栏推荐
- Common open source codeless testing tools
- 都说软件测试很简单有手就行,但为何仍有这么多劝退的?
- 特征缩放 标准化 归一化
- Concurrent optimization summary
- UML图记忆技巧
- 攻防世界 misc 进阶区 2017_Dating_in_Singapore
- Short video system source code, click the blank space of the screen, the keyboard does not automatically stow
- Google Earth Engine(GEE)——以MODIS/006/MCD19A2为例批量下载逐天AOD数据逐天的均值、最大值、最小值、标准差、方差统计分析和CSV下载(北京市各区为例)
- Naacl-22 | introduce the setting of migration learning on the prompt based text generation task
- Taobao commodity review API interface (item_review get Taobao commodity review API interface), tmall commodity review API interface
猜你喜欢

Logo special training camp section III initial creative techniques

MYSQL架构——用户权限与管理

Unity-VScode-Emmylua配置报错解决

Naacl-22 | introduce the setting of migration learning on the prompt based text generation task

Hit the core in the advanced area of misc in the attack and defense world

Attack and defense world misc advanced area ditf

Lost in the lock world of MySQL

Attack and defense world misc advanced area Hong

Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf

MySQL Architecture - logical architecture
随机推荐
The overview and definition of clusters can be seen at a glance
虚拟人产业面临的挑战
Solana chain application crema was shut down due to hacker attacks
Business is too busy. Is there really no reason to have time for automation?
Hit the core in the advanced area of misc in the attack and defense world
Wake up day, how do I step by step towards the road of software testing
Prosperity is exhausted, things are right and people are wrong: where should personal webmasters go
LOGO特訓營 第一節 鑒別Logo與Logo設計思路
好用app推荐:扫描二维码、扫描条形码并查看历史
md5工具类
idea中pom.xml依赖无法导入
攻防世界 MISC 进阶区 hit-the-core
Logo special training camp Section IV importance of font design
sobel过滤器
Force buckle 3_ 383. Ransom letter
页面关闭前,如何发送一个可靠请求
Now MySQL cdc2.1 is parsing the datetime class with a value of 0000-00-00 00:00:00
Erik baleog and Olaf, advanced area of misc in the attack and defense world
Easy to use app recommendation: scan QR code, scan barcode and view history
安装人大金仓数据库