当前位置:网站首页>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;
}
}
边栏推荐
- Why is Dameng data called the "first share" of domestic databases?
- 蓝队攻防演练中的三段作战
- Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)
- Detailed explanation of flask context
- Feature scaling normalization
- Install the gold warehouse database of NPC
- 【OpenGL】笔记二十九、抗锯齿(MSAA)
- UML图记忆技巧
- Logo special training camp section II collocation relationship between words and graphics
- 安装人大金仓数据库
猜你喜欢
[the 2023 autumn recruitment of MIHA tour] open [the only exclusive internal push code of school recruitment eytuc]

Li Kou 98: verify binary search tree

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

10 schemes to ensure interface data security

The Sandbox 和数字好莱坞达成合作,通过人力资源开发加速创作者经济的发展

Logo special training camp section III initial creative techniques

2022-07-04:以下go语言代码输出什么?A:true;B:false;C:编译错误。 package main import “fmt“ func main() { fmt.Pri

攻防世界 MISC 進階區 Erik-Baleog-and-Olaf

On-off and on-off of quality system construction

NFT Insider #64:电商巨头eBay提交NFT相关商标申请,毕马威将在Web3和元宇宙中投入3000万美元
随机推荐
Locust performance test - environment construction and use
The difference between Max and greatest in SQL
LOGO特訓營 第三節 首字母創意手法
Naacl-22 | introduce the setting of migration learning on the prompt based text generation task
Detailed explanation of heap sort code
繁華落盡、物是人非:個人站長該何去何從
idea中pom.xml依赖无法导入
2022-07-04: what is the output of the following go language code? A:true; B:false; C: Compilation error. package main import “fmt“ func main() { fmt.Pri
Attack and defense world misc advanced area ditf
Persistence mechanism of redis
How to reset the password of MySQL root account
蓝队攻防演练中的三段作战
Sword finger offer 67 Convert a string to an integer
Google Earth engine (GEE) -- take modis/006/mcd19a2 as an example to batch download the daily mean, maximum, minimum, standard deviation, statistical analysis of variance and CSV download of daily AOD
华泰证券是国家认可的券商吗?开户安不安全?
攻防世界 MISC 进阶区 Ditf
Unity-VScode-Emmylua配置报错解决
LOGO特训营 第三节 首字母创意手法
Attack and defense world misc master advanced zone 001 normal_ png
[cooking record] - stir fried 1000 pieces of green pepper