当前位置:网站首页>235. 二叉搜索樹的最近公共祖先【lca模板 + 找路徑相同】
235. 二叉搜索樹的最近公共祖先【lca模板 + 找路徑相同】
2022-07-03 18:43:00 【白速龍王的回眸】

分析
找到root到p和q的路徑(通過while和大小關系即可)
然後看看兩條路徑最深能到哪裏相同
返回最深的相同的比特置即可
ac code
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
# 找到root到target的path
def get_path(start, target):
now = start
path = []
while now != target:
path.append(now)
# 下一層尋找
if target.val > now.val:
now = now.right
else:
now = now.left
path.append(target)
return path
pa = get_path(root, p)
pb = get_path(root, q)
ans = None
for u, v in zip(pa, pb):
if u == v:
ans = u
else:
break
return ans
總結
lca模板
边栏推荐
- Theoretical description of linear equations and summary of methods for solving linear equations by eigen
- Suffix derivation based on query object fields
- [Yu Yue education] world reference materials of Microbiology in Shanghai Jiaotong University
- SQL custom collation
- Sensor debugging process
- Grammaire anglaise Nom - Classification
- [Yu Yue education] theoretical mechanics reference materials of Shanghai Jiaotong University
- Sepconv (separable revolution) code recurrence
- Change is the eternal theme
- Web3 credential network project galaxy is better than nym?
猜你喜欢

FBI 警告:有人利用 AI 换脸冒充他人身份进行远程面试

Mature port AI ceaspectus leads the world in the application of AI in terminals, CIMC Feitong advanced products go global, smart terminals, intelligent ports, intelligent terminals

Computer graduation design PHP makeup sales Beauty shopping mall

235. 二叉搜索树的最近公共祖先【lca模板 + 找路径相同】

Ping problem between virtual machine and development board

English语法_形容词/副词3级 - 倍数表达

Compose LazyColumn 顶部添加控件
![[leetcode周赛]第300场——6110. 网格图中递增路径的数目-较难](/img/8d/0e515af6c17971ddf461e3f3b87c30.png)
[leetcode周赛]第300场——6110. 网格图中递增路径的数目-较难

Unity webgl optimization

MySQL duplicate check
随机推荐
PHP determines which constellation it belongs to today
Change is the eternal theme
4. Load balancing and dynamic static separation
Torch learning notes (1) -- 19 common ways to create tensor
Change the single node of Postgres database into master-slave
Administrative division code acquisition
Torch learning notes (5) -- autograd
[combinatorics] generating function (use generating function to solve the number of solutions of indefinite equation)
English grammar_ Adjective / adverb Level 3 - multiple expression
English grammar_ Noun classification
知其然,而知其所以然,JS 对象创建与继承【汇总梳理】
Common PostgreSQL commands
leetcode:556. Next larger element III [simulation + change as little as possible]
Usage of laravel conditional array in
[combinatorics] generating function (use generating function to solve the combination number of multiple sets R)
Recommend a simple browser tab
Raft 日志复制
Data analysis is popular on the Internet, and the full version of "Introduction to data science" is free to download
[Yu Yue education] theoretical mechanics reference materials of Shanghai Jiaotong University
Getting started with JDBC