当前位置:网站首页>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;
}
}
边栏推荐
- 攻防世界 MISC 进阶 glance-50
- 繁华落尽、物是人非:个人站长该何去何从
- Now MySQL cdc2.1 is parsing the datetime class with a value of 0000-00-00 00:00:00
- Install the gold warehouse database of NPC
- Concurrent optimization summary
- Analog rocker controlled steering gear
- Alibaba launched a new brand "Lingyang" and is committed to becoming a "digital leader"
- Concurrent network modular reading notes transfer
- 集群的概述与定义,一看就会
- La prospérité est épuisée, les choses sont bonnes et mauvaises: Où puis - je aller pour un chef de station personnel?
猜你喜欢

10 schemes to ensure interface data security

Serial port data frame
Redis sentinel simply looks at the trade-offs between distributed high availability and consistency

Domestic database chaos

Logo special training camp section III initial creative techniques

Logo Camp d'entraînement section 3 techniques créatives initiales

Attack and defense world misc master advanced zone 001 normal_ png

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

Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf

Unity-VScode-Emmylua配置报错解决
随机推荐
剑指 Offer 65. 不用加减乘除做加法
Gnawing down the big bone - sorting (II)
面试必备 LeetCode 链表算法题汇总,全程干货!
About stack area, heap area, global area, text constant area and program code area
Sword finger offer 65 Add without adding, subtracting, multiplying, dividing
Wake up day, how do I step by step towards the road of software testing
微服务--开篇
Create Ca and issue certificate through go language
【lua】int64的支持
攻防世界 misc 高手进阶区 a_good_idea
idea中pom.xml依赖无法导入
Microservices -- Opening
Advanced area of attack and defense world misc 3-11
Solana chain application crema was shut down due to hacker attacks
LOGO特訓營 第一節 鑒別Logo與Logo設計思路
Lost in the lock world of MySQL
Concurrent network modular reading notes transfer
The Sandbox 和数字好莱坞达成合作,通过人力资源开发加速创作者经济的发展
Detailed explanation of heap sort code
Jvm-Sandbox-Repeater的部署