当前位置:网站首页>235. 二叉搜索树的最近公共祖先【lca模板 + 找路径相同】
235. 二叉搜索树的最近公共祖先【lca模板 + 找路径相同】
2022-07-03 18:38: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模板
边栏推荐
- Redis cache avalanche, penetration, breakdown
- Shell script return value with which output
- Prototype inheritance..
- 12、 Service management
- Use of unsafe class
- Setinterval CPU intensive- Is setInterval CPU intensive?
- English grammar_ Noun classification
- CV in transformer learning notes (continuously updated)
- Mysql45 lecture learning notes (II)
- 042. (2.11) do it when it's time to do it
猜你喜欢
![[untitled]](/img/83/5a57ed90aaafde94db600246256867.jpg)
[untitled]

VLAN experiment

How to track the real-time trend of Bank of London

How to analyze the rising and falling rules of London gold trend chart

MySQL duplicate check

Read the paper glodyne global topology preserving dynamic network embedding

Have you learned the correct expression posture of programmers on Valentine's day?
![[leetcode周赛]第300场——6110. 网格图中递增路径的数目-较难](/img/8d/0e515af6c17971ddf461e3f3b87c30.png)
[leetcode周赛]第300场——6110. 网格图中递增路径的数目-较难

NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线

How does GCN use large convolution instead of small convolution? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)
随机推荐
Closure and closure function
2022-2028 global petroleum pipe joint industry research and trend analysis report
Have you learned the correct expression posture of programmers on Valentine's day?
Class exercises
The vscode code is automatically modified to a compliance code when it is formatted and saved
How about the Moco model?
Image 24 bit depth to 8 bit depth
English grammar_ Adjective / adverb Level 3 - multiple expression
Raft 日志复制
Computer graduation project PHP library book borrowing management system
[combinatorics] exponential generating function (example 2 of solving multiple set permutation with exponential generating function)
English語法_名詞 - 分類
[enumeration] annoying frogs always step on my rice fields: (who is the most hateful? (POJ hundred practice 2812)
[Godot] add menu button
Okaleido, a multimedia NFT aggregation platform, is about to go online, and a new NFT era may come
[combinatorics] exponential generating function (proving that the exponential generating function solves the arrangement of multiple sets)
Bloom filter [proposed by bloom in 1970; redis cache penetration solution]
Kratos微服务框架下实现CQRS架构模式
Image 24 bits de profondeur à 8 bits de profondeur
What kind of experience is it when the Institute earns 20000 yuan a month?