当前位置:网站首页>235. Ancêtre public le plus proche de l'arbre de recherche binaire [modèle LCA + même chemin de recherche]
235. Ancêtre public le plus proche de l'arbre de recherche binaire [modèle LCA + même chemin de recherche]
2022-07-03 18:43:00 【Rétrospective du roi dragon blanc】

Analyse
TrouverrootÀpEtqChemin vers(AdoptionwhileEn fonction de la taille)
Et voir où les deux chemins sont les plus profonds
Retourner au même endroit le plus profond
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':
# TrouverrootÀtargetDepath
def get_path(start, target):
now = start
path = []
while now != target:
path.append(now)
# Recherche au niveau suivant
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
Résumé
lcaModèle
边栏推荐
- Database creation, addition, deletion, modification and query
- 042. (2.11) do it when it's time to do it
- 189. Rotation array
- 2022-2028 global lithium battery copper foil industry research and trend analysis report
- 论文阅读 GloDyNE Global Topology Preserving Dynamic Network Embedding
- How do microservices aggregate API documents? This wave of operation is too good
- Computer graduation design PHP sports goods online sales system website
- [combinatorics] generating function (example of using generating function to solve the number of solutions of indefinite equation)
- Typescript configuration
- 22.2.14 -- station B login with code -for circular list form - 'no attribute' - 'needs to be in path selenium screenshot deviation -crop clipping error -bytesio(), etc
猜你喜欢

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

English語法_名詞 - 分類
![[Godot] add menu button](/img/44/ef1e6ac25bcbc8cc3ecc00c52f8ee2.jpg)
[Godot] add menu button

What kind of experience is it when the Institute earns 20000 yuan a month?

2022-2028 global copper foil (thickness 12 μ M) industry research and trend analysis report
知其然,而知其所以然,JS 对象创建与继承【汇总梳理】

Sensor debugging process

CV in transformer learning notes (continuously updated)

CTO and programmer were both sentenced for losing control of the crawler

An academic paper sharing and approval system based on PHP for computer graduation design
随机推荐
Xception for deeplab v3+ (including super detailed code comments and original drawing of the paper)
[combinatorics] generating function (positive integer splitting | basic model of positive integer splitting | disordered splitting with restrictions)
Kratos微服务框架下实现CQRS架构模式
简述服务量化分析体系
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
042. (2.11) do it when it's time to do it
Raft log replication
235. 二叉搜索树的最近公共祖先【lca模板 + 找路径相同】
Recent learning experience
Multifunctional web file manager filestash
Torch learning notes (3) -- univariate linear regression model (self training)
[combinatorics] generating function (use generating function to solve the number of solutions of indefinite equation example 2 | extended to integer solution)
Use of unsafe class
Naoqi robot summary 27
ES7 - Optimization of promise
Okaleido, a multimedia NFT aggregation platform, is about to go online, and a new NFT era may come
Software development freelancer's Road
Unsafe类的使用
Briefly describe the quantitative analysis system of services
平淡的生活里除了有扎破皮肤的刺,还有那些原本让你魂牵梦绕的诗与远方