当前位置:网站首页>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;
}
}
边栏推荐
- Easy to use app recommendation: scan QR code, scan barcode and view history
- Practice and principle of PostgreSQL join
- Unity vscode emmylua configuration error resolution
- It is said that software testing is very simple, but why are there so many dissuasions?
- Prosperity is exhausted, things are right and people are wrong: where should personal webmasters go
- LOGO特訓營 第一節 鑒別Logo與Logo設計思路
- SQL中MAX与GREATEST的区别
- Test will: bug classification and promotion solution
- About stack area, heap area, global area, text constant area and program code area
- Serial port data frame
猜你喜欢

Install the gold warehouse database of NPC

串口数据帧

More than 30 institutions jointly launched the digital collection industry initiative. How will it move forward in the future?

Introducing QA into the software development lifecycle is the best practice that engineers should follow

NFT Insider #64:电商巨头eBay提交NFT相关商标申请,毕马威将在Web3和元宇宙中投入3000万美元

Logo special training camp section II collocation relationship between words and graphics

Unity-VScode-Emmylua配置报错解决

Attack and defense world misc advanced zone 2017_ Dating_ in_ Singapore

Close system call analysis - Performance Optimization

Unity修仙手游 | lua动态滑动功能(3种源码具体实现)
随机推荐
Advanced area a of attack and defense world misc Masters_ good_ idea
剑指 Offer 67. 把字符串转换成整数
Unity vscode emmylua configuration error resolution
Logo special training camp Section IV importance of font design
Redis sentinel simply looks at the trade-offs between distributed high availability and consistency
通过Go语言创建CA与签发证书
9 - 类
特征缩放 标准化 归一化
微服务--开篇
leetcode 72. Edit distance edit distance (medium)
About stack area, heap area, global area, text constant area and program code area
环境加密技术解析
Now MySQL cdc2.1 is parsing the datetime class with a value of 0000-00-00 00:00:00
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
Analog rocker controlled steering gear
Create Ca and issue certificate through go language
【lua】int64的支持
Introducing QA into the software development lifecycle is the best practice that engineers should follow
MySQL Architecture - user rights and management
Recommendation of mobile app for making barcode