当前位置:网站首页>LC236. 二叉树的最近公共祖先
LC236. 二叉树的最近公共祖先
2022-06-29 08:41:00 【996冲冲冲】

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: #放在这,找到了一个后面以这个节点为根节点的子树都不用访问了。因为:
return root #情况1 : 另一个值在当前找到节点的子树上,那么当前节点就是答案
left = recur(root.left,p,q) #情况2 : 另一个值不在当前节点的子树上,那么当前节点的子树也不用访问直接找其他地方
right = recur(root.right,p,q)
if left and right: #放在后序位置,可以通过返回值把结果的信息向前传递
return root
return left if left else right
return recur(root,p,q)
边栏推荐
猜你喜欢

3DMax 卡死、白屏、渲染死机问题总结

深卷积神经网络时代的目标检测研究进展

Remember to customize the top navigation bar of wechat applet

Wechat applet project: tab navigation bar

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

Instance error iopub data rate exceeded

Lffd: a lightweight fast face detector for edge detection

商业智能BI的未来,如何看待AI+BI这种模式?

YOLACT实时实例分割

How to do unit test well
随机推荐
jar包和war包
UE4 在4.20-23版本安裝Datasmith插件
微信小程序分享页面,分享到朋友圈
Yotact real-time instance segmentation
专业结构record
LFFD:一种用于边缘检测的轻量化快速人脸检测器
什么是超融合?与传统架构有什么区别?.
Mongodb persistence
爱快安装或重置后,PC或手机端获取不到ip
微信小程序判断url的文件格式
Pytorch learning summary - memory cost of operation
Debugging H5 page -weinre and spy debugger real machine debugging
Recursive RBAC menu level display infinite classification
UE4 在viewport视口中显示3D可编辑点
How to implement observer mode
GPU training cloud platform record
Summary of 3DMAX jamming, white screen and rendering crash
pytorch总结—自动求梯度
MH/T 6040航空材料烟密度试验
《网络是怎么样连接的》读书笔记 - WEB服务端请求和响应(五)