当前位置:网站首页>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;
}
}
边栏推荐
- Now MySQL cdc2.1 is parsing the datetime class with a value of 0000-00-00 00:00:00
- 都说软件测试很简单有手就行,但为何仍有这么多劝退的?
- Apachecn translation, proofreading, note sorting activity progress announcement 2022.7
- Logo special training camp section 1 Identification logo and logo design ideas
- Deployment of JVM sandbox repeater
- Introduction and application of bigfilter global transaction anti duplication component
- md5工具类
- In Linux, I call odspcmd to query the database information. How to output silently is to only output values. Don't do this
- 攻防世界 MISC 进阶 glance-50
- leetcode 72. Edit Distance 编辑距离(中等)
猜你喜欢
Wake up day, how do I step by step towards the road of software testing
Install the gold warehouse database of NPC
Challenges faced by virtual human industry
Advanced area of attack and defense world misc 3-11
堆排序代码详解
On-off and on-off of quality system construction
Mongodb aggregation operation summary
Attack and defense world misc advanced grace-50
攻防世界 MISC 進階區 Erik-Baleog-and-Olaf
安装人大金仓数据库
随机推荐
安装人大金仓数据库
Flask 上下文详解
攻防世界 MISC 高手进阶区 001 normal_png
Google Earth Engine(GEE)——以MODIS/006/MCD19A2为例批量下载逐天AOD数据逐天的均值、最大值、最小值、标准差、方差统计分析和CSV下载(北京市各区为例)
剑指 Offer 65. 不用加减乘除做加法
How diff are the contents of the same configuration item in different environments?
String类中的常用方法
POM in idea XML dependency cannot be imported
Attack and defense world misc advanced area Hong
MySQL storage data encryption
攻防世界 MISC 進階區 Erik-Baleog-and-Olaf
串口数据帧
MD5 tool class
Persistence mechanism of redis
Create Ca and issue certificate through go language
Record: how to scroll screenshots of web pages on Microsoft edge in win10 system?
Domestic database chaos
Attack and defense world misc advanced zone 2017_ Dating_ in_ Singapore
Unity vscode emmylua configuration error resolution
Business is too busy. Is there really no reason to have time for automation?