当前位置:网站首页>Sword finger offer 68 - ii The nearest common ancestor of binary tree
Sword finger offer 68 - ii The nearest common ancestor of binary tree
2022-07-04 22:45:00 【LuZhouShiLi】
The finger of the sword Offer 68 - II. The nearest common ancestor of a binary tree
subject
Given a binary tree , Find the nearest common ancestor of the two specified nodes in the tree . In Baidu Encyclopedia, the most recent definition of public ancestor is :“ For a tree T Two nodes of p、q, Recently, the common ancestor is represented as a node x, Satisfy x yes p、q Our ancestors and x As deep as possible ( A node can also be its own ancestor ).”
Ideas
- If P and Q All are root Left and right nodes of , that root It's the nearest public ancestor we're looking for
- If P and Q All are root The left node , Then the return lowestCommonAncestor(root.left,p,q)
- If P and Q All are root The right of the node , Then the return lowestCommonAncestor(root.right,p,q)
Boundary conditions :
- If root = null That means we can't find , If root = p perhaps root = q, return root
- If the left tree is not found , Recursive functions return null, prove p and q They are all tested at the same time , Then look in the right subtree
- If the right subtree is not found , Recursive functions return null, prove p and q They are all tested at the same time , Then look in the left subtree
Code
/** * 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;
}
}
边栏推荐
- Attack and defense world misc advanced area Hong
- 攻防世界 MISC 进阶区 Ditf
- The new version judges the code of PC and mobile terminal, the mobile terminal jumps to the mobile terminal, and the PC jumps to the latest valid code of PC terminal
- 繁華落盡、物是人非:個人站長該何去何從
- Now MySQL cdc2.1 is parsing the datetime class with a value of 0000-00-00 00:00:00
- 繁华落尽、物是人非:个人站长该何去何从
- Interview essential leetcode linked list algorithm question summary, whole process dry goods!
- Shell script implements application service log warehousing MySQL
- idea中pom.xml依赖无法导入
- Erik baleog and Olaf, advanced area of misc in the attack and defense world
猜你喜欢
How to send a reliable request before closing the page

Unity-VScode-Emmylua配置报错解决

MySQL Architecture - logical architecture

攻防世界 MISC 进阶区 can_has_stdio?

Google Earth Engine(GEE)——基于 MCD64A1 的 GlobFire 日常火灾数据集

攻防世界 misc 进阶区 2017_Dating_in_Singapore

NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse

Why is Dameng data called the "first share" of domestic databases?

Wake up day, how do I step by step towards the road of software testing

攻防世界 misc 高手进阶区 a_good_idea
随机推荐
LOGO特训营 第五节 字体结构与设计常用技法
md5工具类
攻防世界 MISC 进阶区 Erik-Baleog-and-Olaf
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
LOGO特训营 第一节 鉴别Logo与Logo设计思路
微服务--开篇
Google Earth Engine(GEE)——以MODIS/006/MCD19A2为例批量下载逐天AOD数据逐天的均值、最大值、最小值、标准差、方差统计分析和CSV下载(北京市各区为例)
Concurrent optimization summary
Introducing QA into the software development lifecycle is the best practice that engineers should follow
攻防世界 misc 高手进阶区 a_good_idea
9 - 类
不同环境相同配置项的内容如何diff差异?
LOGO special training camp section I identification logo and Logo Design Ideas
[the 2023 autumn recruitment of MIHA tour] open [the only exclusive internal push code of school recruitment eytuc]
Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)
Attack and defense world misc advanced area ditf
Is Huatai Securities a nationally recognized securities firm? Is it safe to open an account?
10 schemes to ensure interface data security
关于栈区、堆区、全局区、文字常量区、程序代码区
Serial port data frame