当前位置:网站首页>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
边栏推荐
- [Yugong series] go teaching course 002 go language environment installation in July 2022
- "Designer universe" APEC safety and health +: environmental protection Panda "xiaobaobao" Happy Valentine's Day 2022 | ChinaBrand | Asia Pacific Economic media
- Notes on MySQL related knowledge points (startup, index)
- Decompile and modify the non source exe or DLL with dnspy
- The "boss management manual" that is wildly spread all over the network (turn)
- Why use pycharm to run the use case successfully but cannot exit?
- A little understanding of GSLB (global server load balance) technology
- Kubernetes abnormal communication network fault solution ideas
- Idea shortcut word operation
- Luogu deep foundation part 1 Introduction to language Chapter 7 functions and structures
猜你喜欢

Hcie security Day10: six experiments to understand VRRP and reliability

内存分析器 (MAT)

Getting started with postman -- built-in dynamic parameters, custom parameters and assertions

Yyds dry inventory hcie security Day12: concept of supplementary package filtering and security policy

Mysql database ----- common commands of database (based on database)

MySQL - database backup

Station B, dark horse programmer, employee management system, access conflict related (there is an unhandled exception at 0x00007ff633a4c54d (in employee management system.Exe): 0xc0000005: read locat

Go learning notes (4) basic types and statements (3)

QT6 QML book/qt quick 3d/ Basics

Après 90 ans, j'ai démissionné pour démarrer une entreprise et j'ai dit que j'allais détruire la base de données Cloud.
随机推荐
The post-90s resigned and started a business, saying they would kill cloud database
抓包整理外篇——————autoResponder、composer 、statistics [ 三]
Décompiler et modifier un exe ou une DLL non source en utilisant dnspy
Intimacy communication -- [repair relationship] - use communication to heal injuries
Design e-commerce seckill system
Ask and answer: dispel your doubts about the virtual function mechanism
Mysql - - Index
Hcie security Day11: preliminarily learn the concepts of firewall dual machine hot standby and vgmp
Luogu deep foundation part 1 Introduction to language Chapter 6 string and file operation
"Actbert" Baidu & Sydney University of technology proposed actbert to learn the global and local video text representation, which is effective in five video text tasks
MySQL——idea连接MySQL
flink sql-client 退出,表就会被清空怎么办?
Scientific research document management Zotero
Après 90 ans, j'ai démissionné pour démarrer une entreprise et j'ai dit que j'allais détruire la base de données Cloud.
Quickly distinguish slices and arrays
UI automation test: selenium+po mode +pytest+allure integration
Transformation between yaml, Jason and Dict
How to choose cache read / write strategies in different business scenarios?
Day 9 HomeWrok-ClassHierarchyAnalysis
An expression that regularly matches one of two strings