当前位置:网站首页>剑指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;
}
}
边栏推荐
- Logo special training camp Section V font structure and common design techniques
- The Sandbox 和数字好莱坞达成合作,通过人力资源开发加速创作者经济的发展
- 通过Go语言创建CA与签发证书
- NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse
- MD5 tool class
- 质量体系建设之路的分分合合
- High school physics: linear motion
- 制作条形码的手机App推荐
- Alibaba launched a new brand "Lingyang" and is committed to becoming a "digital leader"
- MYSQL架构——逻辑架构
猜你喜欢
攻防世界 MISC 高手进阶区 001 normal_png
sobel过滤器
SPSS安装激活教程(包含网盘链接)
Attack and defense world misc advanced grace-50
PMO: compare the sample efficiency of 25 molecular optimization methods
Tiktok actual combat ~ the number of comments is updated synchronously
It is said that software testing is very simple, but why are there so many dissuasions?
LOGO special training camp section I identification logo and Logo Design Ideas
Logo special training camp section III initial creative techniques
UML图记忆技巧
随机推荐
Domestic database chaos
Summary of index operations in mongodb
It is said that software testing is very simple, but why are there so many dissuasions?
Redis sentinel simply looks at the trade-offs between distributed high availability and consistency
LOGO特訓營 第三節 首字母創意手法
Logo special training camp section III initial creative techniques
LOGO特训营 第四节 字体设计的重要性
安装人大金仓数据库
Detailed explanation of heap sort code
测试必会:BUG的分类及推进解决
新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码
How diff are the contents of the same configuration item in different environments?
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
MYSQL架构——逻辑架构
将QA引入软件开发生命周期是工程师要遵循的最佳实践
The sandbox has reached a cooperation with digital Hollywood to accelerate the economic development of creators through human resource development
环境加密技术解析
Analog rocker controlled steering gear
La prospérité est épuisée, les choses sont bonnes et mauvaises: Où puis - je aller pour un chef de station personnel?
The table is backed up in ODPs. Why check m in the metabase_ Table, the logical sizes of the two tables are inconsistent, but the number of