当前位置:网站首页>76.二叉树的最近公共祖先
76.二叉树的最近公共祖先
2022-06-29 18:29:00 【是乔乔啊】
题目

思路

代码
class Solution:
def lowestCommonAncestor(self, root: TreeNode, p: TreeNode, q: TreeNode) -> TreeNode:
if not root or root == p or root == q:
return root
left = self.lowestCommonAncestor(root.left, p, q)
right = self.lowestCommonAncestor(root.right, p, q)
if not left and not right:
return # 1.
if not left:
return right # 3.
if not right:
return left # 4.
return root # 2. if left and right:
边栏推荐
- How do I add SmartArt to slides in PowerPoint?
- Shandong University project training (VII) add navigation bar to select city
- Chapter 02_ MySQL data directory
- 2. add customized related files to the keil5 project established by stm32cubemx
- 报错[warning] Neural network inference was performed on socket ‘RGB’, depth frame is aligned to socket
- Understanding of strong caching and negotiation caching
- MySQL - clear data in the table
- Cannot retrieve repository metadata processing records
- How to use an oak camera as a webcam?
- Adobe Premiere foundation - opacity (mixed mode) (XII)
猜你喜欢

Know that Chuangyu has helped the energy industry in asset management and was selected into the 2021 IOT demonstration project of the Ministry of industry and information technology

These advantages of the Institute are really fragrant! The landing rate is still very high!

山东大学项目实训(七)添加导航栏选择城市

Anaconda安装并配置jupyter notebook远程

How to use idea?

The 8th "Internet +" competition - cloud native track invites you to challenge

Fluent's MSH grid learning

Adobe Premiere foundation - opacity (mixed mode) (XII)

Machine learning 7-Support vector machine

Shandong University project training (VII) add navigation bar to select city
随机推荐
Amazing pandaverse:meta "borderless, to activate fashion attributes in the new journey of 2.0
面试题 10.10. 数字流的秩
数据分析基础--预测模型
How to use an oak camera as a webcam?
Up to 81.98%! Announcement of undergraduate study rate of more than 100 "double first-class" Universities
1. 使用STM32CubeMX建立STM32G030C8T6项目工程
Cannot retrieve repository metadata processing records
Data analysis time series prediction
idea怎么使用?
What is a multi paradigm programming language and what does "multi paradigm" mean?
Adobe Premiere基础-不透明度(蒙版)(十一)
[daily training] 535 Encryption and decryption of tinyurl
6.29模拟赛总结
2022.6.29-----leetcode.535
mysql -connector/j驱动下载
SD6.24集训总结
SD6.23集训总结
Redis (I) -- getting started with redis (2) -- redis data type
Leetcode 984. String without AAA or BBB (thought of netizens)
How to use the low code platform of the Internet of things for service management?