当前位置:网站首页>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模板
边栏推荐
- Why can deeplab v3+ be a God? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)
- Torch learning notes (7) -- take lenet as an example for dataload operation (detailed explanation + reserve knowledge supplement)
- 2022.02.11
- 2022-2028 global marking ink industry research and trend analysis report
- [Yu Yue education] theoretical mechanics reference materials of Shanghai Jiaotong University
- Suffix derivation based on query object fields
- Enterprise custom form engine solution (12) -- form rule engine 2
- Boost.Asio Library
- SSH 远程执行命令简介
- 199. Right view of binary tree - breadth search
猜你喜欢

Torch learning notes (3) -- univariate linear regression model (self training)

Compose LazyColumn 顶部添加控件
![leetcode:11. Container with the most water [double pointer + greed + remove the shortest board]](/img/d4/cbbaec40119be6cb5594899e348261.png)
leetcode:11. Container with the most water [double pointer + greed + remove the shortest board]

Data analysis is popular on the Internet, and the full version of "Introduction to data science" is free to download

What London Silver Trading software supports multiple languages

Sensor 调试流程

2022-2028 global physiotherapy clinic industry research and trend analysis report

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

How do microservices aggregate API documents? This wave of operation is too good

NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
随机推荐
Suffix derivation based on query object fields
Briefly describe the quantitative analysis system of services
English語法_名詞 - 分類
leetcode:11. Container with the most water [double pointer + greed + remove the shortest board]
MySQL duplicate check
Unity webgl optimization
Torch learning notes (2) -- 11 common operation modes of tensor
How to read the source code [debug and observe the source code]
Unsafe类的使用
[combinatorics] exponential generating function (properties of exponential generating function | exponential generating function solving multiple set arrangement)
2022-2028 global physiotherapy clinic industry research and trend analysis report
2022-2028 global sepsis treatment drug industry research and trend analysis report
189. Rotation array
199. Right view of binary tree - breadth search
Pan for in-depth understanding of the attention mechanism in CV
[combinatorics] generating function (use generating function to solve the number of solutions of indefinite equation)
There are several levels of personal income tax
The vscode code is automatically modified to a compliance code when it is formatted and saved
Raft log replication
Sensor debugging process