当前位置:网站首页>Leetcode problem solving - 235 Nearest common ancestor of binary search tree
Leetcode problem solving - 235 Nearest common ancestor of binary search tree
2022-07-03 21:39:00 【Snowy solitary boat】
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
TreeNode max = p.val>q.val?p:q;
TreeNode min = p.val<q.val?p:q;
while (root!=null){
if (root.val>=min.val&&root.val<=max.val) return root;
if (root.val>max.val) root = root.left;
if (root.val<min.val) root = root.right;
}
return root;
}
Ideas :
Based on the characteristics of binary search tree, we iterate layer by layer , Finally, when the current node value is between two node values , It is the common parent node of these two nodes
边栏推荐
- MySQL - index
- Is it OK for fresh students to change careers to do software testing? The senior answered with his own experience
- 运维各常用命令总结
- Talk about daily newspaper design - how to write a daily newspaper and what is the use of a daily newspaper?
- Are the top ten securities companies safe to open accounts and register? Is there any risk?
- MySQL——SQL注入问题
- Qualcomm platform WiFi -- P2P issue
- Netfilter ARP log
- Getting started with postman -- built-in dynamic parameters, custom parameters and assertions
- Goodbye 2021, how do programmers go to the top of the disdain chain?
猜你喜欢

Basic preprocessing and data enhancement of image data

常用sql集合
Implementation principle of inheritance, encapsulation and polymorphism

Single page application architecture

仿网易云音乐小程序

运维各常用命令总结

Collections SQL communes

Etcd raft Based Consistency assurance

Advanced collaboration: coroutinecontext

Study diary: February 14th, 2022
随机推荐
Pengcheng cup Web_ WP
leetcode-540. A single element in an ordered array
大神们,我想发两个广播流1 从mysql加载基础数据,广播出去2 从kafka加载基础数据的变更
Compréhension de la technologie gslb (Global Server load balance)
Custom view incomplete to be continued
内存分析器 (MAT)
QT6 QML book/qt quick 3d/ Basics
Mysql database ----- common commands of database (based on database)
Monkey/ auto traverse test, integrate screen recording requirements
Basic preprocessing and data enhancement of image data
常用sql集合
How to install sentinel console
What should the future of the Internet be like when Silicon Valley employees flee the big factory and rush to Web3| Footprint Analytics
全网都在疯传的《老板管理手册》(转)
MySQL - SQL injection problem
Redis concludes that the second pipeline publishes / subscribes to bloom filter redis as a database and caches RDB AOF redis configuration files
Teach you how to install aidlux (1 installation)
treevalue——Master Nested Data Like Tensor
MySQL - idea connects to MySQL
gslb(global server load balance)技術的一點理解