当前位置:网站首页>Sword finger offer 68 - I. nearest common ancestor of binary search tree
Sword finger offer 68 - I. nearest common ancestor of binary search tree
2022-07-04 22:45:00 【LuZhouShiLi】
The finger of the sword Offer 68 - I. The nearest common ancestor of a binary search tree
subject
Given a binary search 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
- When p,q All in root In the right subtree , The traverse root.right
- When p,q All in root The left subtree , The traverse root.left
- When p,q stay root Both sides , It means that we have found the nearest common ancestor
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) {
while(root != null)
{
if(root.val < p.val && root.val < q.val)
{
// Byte point ratio root It's worth less explain pq In the left sub tree Traverse left subtree
root = root.right;
}
else if(root.val > p.val && root.val > q.val)
{
root = root.left;
}
else{
break;// p q stay root Left and right explain root The most recent public ancestor Direct jump out
}
}
return root;
}
}
边栏推荐
- 剑指 Offer 67. 把字符串转换成整数
- sobel过滤器
- Detailed explanation of heap sort code
- Gnawing down the big bone - sorting (II)
- Unity修仙手游 | lua动态滑动功能(3种源码具体实现)
- How to send a reliable request before closing the page
- Lost in the lock world of MySQL
- Redis的持久化机制
- Logo special training camp section III initial creative techniques
- 醒悟的日子,我是怎么一步一步走向软件测试的道路
猜你喜欢
攻防世界 MISC 进阶区 hong
On-off and on-off of quality system construction
页面关闭前,如何发送一个可靠请求
攻防世界 MISC 进阶 glance-50
LOGO special training camp section I identification logo and Logo Design Ideas
Close system call analysis - Performance Optimization
Logo special training camp section 1 Identification logo and logo design ideas
Advanced area a of attack and defense world misc Masters_ good_ idea
攻防世界 MISC 高手进阶区 001 normal_png
The overview and definition of clusters can be seen at a glance
随机推荐
SPSS installation and activation tutorial (including network disk link)
MYSQL架构——用户权限与管理
Logo special training camp Section V font structure and common design techniques
Naacl-22 | introduce the setting of migration learning on the prompt based text generation task
Mysql root 账号如何重置密码
攻防世界 misc 高手进阶区 a_good_idea
高中物理:直线运动
9 - 类
Sobel filter
Concurrent optimization summary
SQL中MAX与GREATEST的区别
Detailed explanation of heap sort code
Wake up day, how do I step by step towards the road of software testing
攻防世界 MISC 进阶区 can_has_stdio?
sobel过滤器
MySQL Architecture - user rights and management
Logo special training camp section II collocation relationship between words and graphics
Alibaba launched a new brand "Lingyang" and is committed to becoming a "digital leader"
攻防世界 MISC 进阶区 hong
LOGO特訓營 第一節 鑒別Logo與Logo設計思路