当前位置:网站首页>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模板
边栏推荐
- [combinatorics] dislocation problem (recursive formula | general term formula | derivation process)*
- Bidding procurement scheme management of Oracle project management system
- How does GCN use large convolution instead of small convolution? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)
- [combinatorics] generating function (positive integer splitting | repeated ordered splitting | non repeated ordered splitting | proof of the number of repeated ordered splitting schemes)
- Torch learning notes (3) -- univariate linear regression model (self training)
- How do microservices aggregate API documents? This wave of operation is too good
- 2022-2028 global copper foil (thickness 12 μ M) industry research and trend analysis report
- Implementation of cqrs architecture mode under Kratos microservice framework
- 毕业总结
- Should I be laid off at the age of 40? IBM is suspected of age discrimination, calling its old employees "dinosaurs" and planning to dismiss, but the employees can't refute it
猜你喜欢
MySQL duplicate check
How to expand the capacity of golang slice slice
Pan for in-depth understanding of the attention mechanism in CV
Grammaire anglaise Nom - Classification
Install apache+php+mysql+phpmyadmin xampp and its error resolution
2022-2028 global copper foil (thickness 12 μ M) industry research and trend analysis report
Add control at the top of compose lazycolumn
[combinatorics] dislocation problem (recursive formula | general term formula | derivation process)*
组策略中开机脚本与登录脚本所使用的用户身份
An academic paper sharing and approval system based on PHP for computer graduation design
随机推荐
CTO and programmer were both sentenced for losing control of the crawler
Getting started with JDBC
leetcode:11. 盛最多水的容器【雙指針 + 貪心 + 去除最短板】
Torch learning notes (4) -- torch's dynamic calculation diagram
Unity webgl optimization
[combinatorics] exponential generating function (properties of exponential generating function | exponential generating function solving multiple set arrangement)
[combinatorics] generating function (positive integer splitting | unordered non repeated splitting example)
Boost.Asio Library
[combinatorics] generating function (positive integer splitting | repeated ordered splitting | non repeated ordered splitting | proof of the number of repeated ordered splitting schemes)
Golang string (string) and byte array ([]byte) are converted to each other
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
毕业总结
变化是永恒的主题
Torch learning notes (7) -- take lenet as an example for dataload operation (detailed explanation + reserve knowledge supplement)
Enterprise custom form engine solution (12) -- form rule engine 2
Data analysis is popular on the Internet, and the full version of "Introduction to data science" is free to download
[combinatorics] exponential generating function (proving that the exponential generating function solves the arrangement of multiple sets)
VLAN experiment
[combinatorics] generating function (positive integer splitting | basic model of positive integer splitting | disordered splitting with restrictions)
042. (2.11) do it when it's time to do it