当前位置:网站首页>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;
}
}
边栏推荐
- Close system call analysis - Performance Optimization
- LOGO特训营 第五节 字体结构与设计常用技法
- Logo special training camp section III initial creative techniques
- Apachecn translation, proofreading, note sorting activity progress announcement 2022.7
- Practice and principle of PostgreSQL join
- Attack and defense world misc advanced area Hong
- MySQL Architecture - user rights and management
- 关于栈区、堆区、全局区、文字常量区、程序代码区
- Test will: bug classification and promotion solution
- PHP short video source code, thumb animation will float when you like it
猜你喜欢

醒悟的日子,我是怎么一步一步走向软件测试的道路

攻防世界 MISC 进阶区 can_has_stdio?

安装人大金仓数据库

蓝队攻防演练中的三段作战

LOGO特训营 第四节 字体设计的重要性

LOGO特训营 第三节 首字母创意手法

Unity修仙手游 | lua动态滑动功能(3种源码具体实现)

The overview and definition of clusters can be seen at a glance

攻防世界 MISC 进阶区 Erik-Baleog-and-Olaf

Wake up day, how do I step by step towards the road of software testing
随机推荐
Shell script implements application service log warehousing MySQL
Lost in the lock world of MySQL
NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse
How diff are the contents of the same configuration item in different environments?
Introducing QA into the software development lifecycle is the best practice that engineers should follow
攻防世界 MISC 进阶区 Ditf
Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)
业务太忙,真的是没时间搞自动化理由吗?
Persistence mechanism of redis
How to reset the password of MySQL root account
LOGO特训营 第三节 首字母创意手法
Flask 上下文详解
In Linux, I call odspcmd to query the database information. How to output silently is to only output values. Don't do this
攻防世界 MISC 進階區 Erik-Baleog-and-Olaf
leetcode 72. Edit distance edit distance (medium)
Logo special training camp section 1 Identification logo and logo design ideas
共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
Create Ca and issue certificate through go language
记录:关于Win10系统中Microsoft Edge上的网页如何滚动截屏?
LOGO特訓營 第一節 鑒別Logo與Logo設計思路