当前位置:网站首页>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;
}
}
边栏推荐
- Taobao commodity review API interface (item_review get Taobao commodity review API interface), tmall commodity review API interface
- UML图记忆技巧
- [Lua] Int64 support
- Sword finger offer 65 Add without adding, subtracting, multiplying, dividing
- About stack area, heap area, global area, text constant area and program code area
- The new version judges the code of PC and mobile terminal, the mobile terminal jumps to the mobile terminal, and the PC jumps to the latest valid code of PC terminal
- Tla+ introductory tutorial (1): introduction to formal methods
- Unity vscode emmylua configuration error resolution
- Erik baleog and Olaf, advanced area of misc in the attack and defense world
- Sobel filter
猜你喜欢

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

【OpenGL】笔记二十九、抗锯齿(MSAA)

Google Earth Engine(GEE)——基于 MCD64A1 的 GlobFire 日常火灾数据集

UML图记忆技巧

攻防世界 MISC 进阶区 hong

共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf

攻防世界 MISC 进阶区 Ditf

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

UML diagram memory skills

LOGO特訓營 第三節 首字母創意手法
随机推荐
LOGO特訓營 第三節 首字母創意手法
Jvm-Sandbox-Repeater的部署
Feature scaling normalization
Solana chain application crema was shut down due to hacker attacks
On-off and on-off of quality system construction
Logo Camp d'entraînement section 3 techniques créatives initiales
Flask 上下文详解
剑指 Offer 65. 不用加减乘除做加法
md5工具类
[cooking record] - stir fried 1000 pieces of green pepper
【lua】int64的支持
Naacl-22 | introduce the setting of migration learning on the prompt based text generation task
Now MySQL cdc2.1 is parsing the datetime class with a value of 0000-00-00 00:00:00
记录:关于Win10系统中Microsoft Edge上的网页如何滚动截屏?
Interview essential leetcode linked list algorithm question summary, whole process dry goods!
Close system call analysis - Performance Optimization
Logo special training camp section 1 Identification logo and logo design ideas
MySQL Architecture - logical architecture
环境加密技术解析
Is Huatai Securities a nationally recognized securities firm? Is it safe to open an account?