当前位置:网站首页>75. nearest common ancestor of binary search tree
75. nearest common ancestor of binary search tree
2022-06-29 18:57:00 【It's Joe Joe】
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
iteration :
1. Loop search : When node root Jump out when empty ;
- When p, q All in root Of Right subtree in , Then traverse to root.right ;
- otherwise , When p, q All in root Of In the left subtree , Then traverse to root.left ;
- otherwise , It means that Recent public ancestor , Jump out of .
2. Return value : Recent public ancestor root .
Code
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
while root:
if root.val < p.val and root.val < q.val: # p,q All in root In the right subtree
root = root.right # Traverse to the right child node
elif root.val > p.val and root.val > q.val: # p,q All in root In the left subtree
root = root.left # Traverse to the left child node
else: break
return root
边栏推荐
- markdown常用字体
- 2. how to install MySQL database in Galaxy Kirin offline mode
- PHP实现二维数组按指定的键名排序的方法
- 75.二叉搜索树额最近公共祖先
- MySQL 企業級開發規範
- [daily training] 535 Encryption and decryption of tinyurl
- Shandong University project training (VIII) design rotation map entry page
- data-*属性用法
- The 8th "Internet +" competition - cloud native track invites you to challenge
- CentOS 7.5 install MySQL 8.0.27---yum
猜你喜欢

RocketMQ的tag过滤和sql过滤

细说GaussDB(DWS)复杂多样的资源负载管理手段
![Error [warning] neural network information was performed on socket 'RGB', depth frame is aligned to socket](/img/8a/ebad75daa581e22d50dddde49e1fac.jpg)
Error [warning] neural network information was performed on socket 'RGB', depth frame is aligned to socket

How to use the low code platform of the Internet of things for service management?

Mac: MySQL 66 questions, 20000 words + 50 pictures!

关于微服务

Markdown knowledge comes gently

Fluent's MSH grid learning

Using protobuf to link MySQL in unrealeengine plug-in

markdown知识轻轻来袭
随机推荐
Elegant writing controller (parameter verification + unified exception handling)
数据分析--时间序列预测
C Primer Plus 第12章_存储类别、链接和内存管理_代码和练习题
centos 7.5安装mysql 8.0.27----yum
Interview question 10.10 Rank of digital stream
JS converts seconds to "2h30min50s" format
Us judge ruled that the former security director of Uber accused of covering up hacking must face fraud charges
防汛救援便携式应急通信系统解决方案
【日常训练】535. TinyURL 的加密与解密
Fastdfs
Markdown knowledge comes gently
细说GaussDB(DWS)复杂多样的资源负载管理手段
Understanding of strong caching and negotiation caching
unittest单元测试框架
第三方工具与框架集成
Leetcode 984. String without AAA or BBB (thought of netizens)
C Primer Plus Chapter 12_ Storage categories, links, and memory management_ Codes and exercises
Apache InLong百万亿级数据流处理
Machine learning 7-Support vector machine
移动端测试