当前位置:网站首页>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
边栏推荐
- Boost.Asio Library
- 组策略中开机脚本与登录脚本所使用的用户身份
- How about the Moco model?
- Bidding procurement scheme management of Oracle project management system
- How to expand the capacity of golang slice slice
- 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
- What kind of experience is it when the Institute earns 20000 yuan a month?
- webcodecs
- Win 11 major updates, new features love love.
- Golang string (string) and byte array ([]byte) are converted to each other
猜你喜欢
Computer graduation project PHP library book borrowing management system
What is SQL get connection
Kratos微服务框架下实现CQRS架构模式
知其然,而知其所以然,JS 对象创建与继承【汇总梳理】
English语法_名词 - 分类
2022-2028 global petroleum pipe joint industry research and trend analysis report
SQL: special update operation
How to analyze the rising and falling rules of London gold trend chart
Transformer T5 model read slowly
Have you learned the correct expression posture of programmers on Valentine's day?
随机推荐
Analysis of the reasons why enterprises build their own software development teams to use software manpower outsourcing services at the same time
2022-2028 global copper foil (thickness 12 μ M) industry research and trend analysis report
Sensor 调试流程
Administrative division code acquisition
Reappearance of ASPP (atlas spatial pyramid pooling) code
How to track the real-time trend of Bank of London
企业级自定义表单引擎解决方案(十二)--表单规则引擎2
2022-2028 global marking ink industry research and trend analysis report
Coordinate layer conversion tool (video)
TypeScript 官网教程
Typescript official website tutorial
2022-2028 global physiotherapy clinic industry research and trend analysis report
Win 11 major updates, new features love love.
组策略中开机脚本与登录脚本所使用的用户身份
Data analysis is popular on the Internet, and the full version of "Introduction to data science" is free to download
Su embedded training - Day10
Xception for deeplab v3+ (including super detailed code comments and original drawing of the paper)
Implementation of cqrs architecture mode under Kratos microservice framework
There are several levels of personal income tax
How to analyze the rising and falling rules of London gold trend chart