当前位置:网站首页>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
边栏推荐
- 抓包整理外篇——————autoResponder、composer 、statistics [ 三]
- Capture de paquets et tri du contenu externe - - autoresponder, composer, statistiques [3]
- (5) Web security | penetration testing | network security operating system database third-party security, with basic use of nmap and masscan
- Capturing and sorting out external articles -- autoresponder, composer, statistics [III]
- Analyse de REF nerf
- Redis concludes that the second pipeline publishes / subscribes to bloom filter redis as a database and caches RDB AOF redis configuration files
- Quickly distinguish slices and arrays
- University of Electronic Science and technology | playback of clustering experience effectively used in reinforcement learning
- Solve the problem that openocd fails to burn STM32 and cannot connect through SWD
- Is flush account opening and registration safe and reliable? Is there any risk?
猜你喜欢
"Designer universe" APEC safety and health +: environmental protection Panda "xiaobaobao" Happy Valentine's Day 2022 | ChinaBrand | Asia Pacific Economic media
仿网易云音乐小程序
What should the future of the Internet be like when Silicon Valley employees flee the big factory and rush to Web3| Footprint Analytics
Capturing and sorting out external articles -- autoresponder, composer, statistics [III]
Talk about daily newspaper design - how to write a daily newspaper and what is the use of a daily newspaper?
Tidb's initial experience of ticdc6.0
Pengcheng cup Web_ WP
Advanced collaboration: coroutinecontext
Getting started with postman -- environment variables and global variables
Compilation Principle -- syntax analysis
随机推荐
MySQL——数据库备份
Capturing and sorting out external articles -- autoresponder, composer, statistics [III]
Getting started with postman -- built-in dynamic parameters, custom parameters and assertions
Talk about daily newspaper design - how to write a daily newspaper and what is the use of a daily newspaper?
Capturing and sorting out external articles -- autoresponder, composer, statistics [III]
How PHP gets all method names of objects
Great gods, I want to send two broadcast streams: 1. Load basic data from MySQL and 2. Load changes in basic data from Kafka
2022-02-15 Daily: 2022 AAAI fellow release
Install and use Chrony, and then build your own time server
Memory analyzer (MAT)
90 後,辭職創業,說要卷死雲數據庫
仿网易云音乐小程序
Persistence of Nacos
leetcode-540. A single element in an ordered array
Getting started with postman -- environment variables and global variables
Volley source code analysis
Ask and answer: dispel your doubts about the virtual function mechanism
UI automation test: selenium+po mode +pytest+allure integration
What if the Flink SQL client exits and the table is emptied?
Last week's content review