当前位置:网站首页>Lc236. nearest common ancestor of binary tree
Lc236. nearest common ancestor of binary tree
2022-06-29 09:39:00 【996 chongchongchong】

def lowestCommonAncestor(self, root, p, q):
""" :type root: TreeNode :type p: TreeNode :type q: TreeNode :rtype: TreeNode """
def recur(root,p,q):
if not root:
return
if root.val == p.val or root.val == q.val: # Put it here , After finding a subtree with this node as the root node, there is no need to access it . because :
return root # situation 1 : The other value is on the subtree of the currently found node , The current node is the answer
left = recur(root.left,p,q) # situation 2 : Another value is not on the subtree of the current node , Then the subtree of the current node can be found elsewhere without access
right = recur(root.right,p,q)
if left and right: # Put it in the following position , The result information can be passed forward through the return value
return root
return left if left else right
return recur(root,p,q)
边栏推荐
- watch监听和computed计算属性的使用和区别
- Laravel 8 enables the order table to be divided by month level
- Closed door cultivation (24) shallow understanding of cross domain problems
- Simplicity Studio无法识别新买的JLink v9解决方法
- Research progress of target detection in the era of deep convolutional neural network
- UE4 material UV texture does not stretch with model scale
- 通识篇:原型设计的认知,设计及最佳实践
- LeetCode刷题——泰波那契数列
- Iso16000-9 testing of volatile organic compounds in building products and furniture
- ThinkPHP 6 uses mongodb
猜你喜欢

Redo after JS rotation view (longer full version, can be run)

UE4 在viewport视口中显示3D可编辑点

How to do unit test well

Making of simple addition calculator based on pyqt5 and QT Designer

Gd32f4xx Ethernet chip (ENC28J60) driver migration

Mysql配置主从数据库

UE4 animation redirection

Ue4 installe le plug - in datasmith dans la version 4.20-23

Laravel 8 enables the order table to be divided by month level

五心公益红红娘团队
随机推荐
云管理平台:9大开源云管理平台(CMP)
es报错NoNodeAvailableException[None of the configured nodes are available:[.127.0.0.1}{127.0.0.1:9300]
SPI drive of lsm6dsl
Modify EXIF information
Twinmotion beginner tutorial
train_ on_ Batch save the image of the loss function change
爱快安装或重置后,PC或手机端获取不到ip
SSD improvements cfenet
Wechat applet opens file stream
Record the field name dynamically modified by SetData of wechat applet
Wechat applet wx Navigateback returns the parameters carried on the previous page
Universal target detection based on region attention
数据治理:数据标准管理(第三篇)
Simplicity Studio无法识别新买的JLink v9解决方法
1424. 对角线遍历 II
Understanding of singleton mode
The former security director of Uber faced fraud allegations and had concealed data leakage incidents
Find the most repeated element in the string
Wechat applet jump to official account image and text content
pytorch学习总结—运算的内存开销