当前位置:网站首页>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)
边栏推荐
- Professional structure record
- 你必须知道的23个最有用的Elasticseaerch检索技巧
- pytorch学习总结—运算的内存开销
- watch监听和computed计算属性的使用和区别
- Factory mode
- 第十二章 信号(二)- 生产者消费者示例
- Wechat applet search keyword highlighting and ctrl+f search positioning
- Write down some written test questions
- UE4 plug-in reports an error cannot open include file: 'modulemanager H 'resolve
- The difference between cokkie and session
猜你喜欢

SSD改进CFENet

Yolo nano: a highly compact one look convolutional neural network for target detection

Share code snippets of wechat applet

【华为认证】HCIA-DATACOM史上最全精选题库(附答案解析)

Gd32f4xx Ethernet Chip (ENC28J60) Drive transplantation

数据处理时代,数据质量建设才是企业的生存之道

UE4 animation redirection

Western Polytechnic University, one of the "seven national defense schools", was attacked by overseas networks

SSD Improvement cfenet

The principle of session and cookie
随机推荐
Find the most repeated element in the string
Yolo nano: a highly compact one look convolutional neural network for target detection
pytorch总结—TENSOR ON GPU
SSD改进CFENet
SSD Improvement cfenet
Fabrication d'une calculatrice d'addition simple basée sur pyqt5 et Qt Designer
用户级线程和内核级线程
Official STM32 chip package download address stm32f10x stm32f40x Download
UE4 VS的Visual Assist插件设置
Wechat applet sharing page, sharing to the circle of friends
数据治理:数据治理在数据中台下的解决方案
【NOI模拟赛】为NOI加点料(重链剖分,线段树)
股票炒股账号开户安全吗?是靠谱的吗?
MH/T 6040航空材料烟密度试验
第十二章 信号(二)- 生产者消费者示例
c语言printf大家族系列
Wechat applet determines the file format of URL
商业智能BI的未来,如何看待AI+BI这种模式?
The principle of session and cookie
Wechat applet project: wechat applet page layout