当前位置:网站首页>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;
}
}
边栏推荐
- Three stage operations in the attack and defense drill of the blue team
- SPSS安装激活教程(包含网盘链接)
- The difference between Max and greatest in SQL
- 啃下大骨头——排序(二)
- The overview and definition of clusters can be seen at a glance
- 攻防世界 MISC 进阶区 Ditf
- 攻防世界 MISC 进阶区 hit-the-core
- 华泰证券是国家认可的券商吗?开户安不安全?
- 繁華落盡、物是人非:個人站長該何去何從
- LOGO特訓營 第三節 首字母創意手法
猜你喜欢
Close system call analysis - Performance Optimization
Concurrent network modular reading notes transfer
Persistence mechanism of redis
攻防世界 MISC 进阶 glance-50
Introducing QA into the software development lifecycle is the best practice that engineers should follow
共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
LOGO特训营 第三节 首字母创意手法
醒悟的日子,我是怎么一步一步走向软件测试的道路
Lost in the lock world of MySQL
Logo special training camp section II collocation relationship between words and graphics
随机推荐
面试必备 LeetCode 链表算法题汇总,全程干货!
Domestic database chaos
Hit the core in the advanced area of misc in the attack and defense world
9 - 类
Logo special training camp section III initial creative techniques
Summary of index operations in mongodb
On-off and on-off of quality system construction
Attack and defense world misc advanced area can_ has_ stdio?
安装人大金仓数据库
Attack and defense world misc advanced zone 2017_ Dating_ in_ Singapore
Taobao commodity review API interface (item_review get Taobao commodity review API interface), tmall commodity review API interface
Short video system source code, click the blank space of the screen, the keyboard does not automatically stow
PostgreSQL server programming aggregation and grouping
Analysis of environmental encryption technology
Apachecn translation, proofreading, note sorting activity progress announcement 2022.7
Advanced area a of attack and defense world misc Masters_ good_ idea
华泰证券是国家认可的券商吗?开户安不安全?
集群的概述与定义,一看就会
Challenges faced by virtual human industry
新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码