当前位置:网站首页>235. The nearest common ancestor of the binary search tree [LCA template + same search path]
235. The nearest common ancestor of the binary search tree [LCA template + same search path]
2022-07-03 18:39:00 【White speed Dragon King's review】
analysis
find root To p and q The path of ( adopt while And size )
Then see where the two paths can reach the same depth
Just return to the same deepest position
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':
# find root To target Of path
def get_path(start, target):
now = start
path = []
while now != target:
path.append(now)
# Next level search
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
summary
lca Templates
边栏推荐
- [combinatorics] generating function (generating function application scenario | using generating function to solve recursive equation)
- Common PostgreSQL commands
- Reappearance of ASPP (atlas spatial pyramid pooling) code
- Introduction to SSH Remote execution command
- What problems can cross-border e-commerce sellers solve with multi platform ERP management system
- What kind of experience is it when the Institute earns 20000 yuan a month?
- Mysql45 lecture learning notes (II)
- English语法_名词 - 分类
- 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
- webcodecs
猜你喜欢
[Yu Yue education] theoretical mechanics reference materials of Shanghai Jiaotong University
2022-2028 global copper foil (thickness 12 μ M) industry research and trend analysis report
FBI 警告:有人利用 AI 换脸冒充他人身份进行远程面试
Computer graduation project PHP library book borrowing management system
After the festival, a large number of people change careers. Is it still time to be 30? Listen to the experience of the past people
Win 11 major updates, new features love love.
Prototype inheritance..
Bidding procurement scheme management of Oracle project management system
Nodejs (01) - introductory tutorial
Kratos微服务框架下实现CQRS架构模式
随机推荐
Ping problem between virtual machine and development board
041. (2.10) talk about manpower outsourcing
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
Chisel tutorial - 06 Phased summary: implement an FIR filter (chisel implements 4-bit FIR filter and parameterized FIR filter)
CV in transformer learning notes (continuously updated)
[combinatorics] generating function (generating function application scenario | using generating function to solve recursive equation)
204. Count prime
企业级自定义表单引擎解决方案(十二)--表单规则引擎2
[combinatorics] exponential generating function (example 2 of solving multiple set permutation with exponential generating function)
There are several levels of personal income tax
042. (2.11) do it when it's time to do it
Raft log replication
Niuke monthly race 31 minus integer
[combinatorics] dislocation problem (recursive formula | general term formula | derivation process)*
JS_ Array_ sort
Torch learning notes (4) -- torch's dynamic calculation diagram
Xception for deeplab v3+ (including super detailed code comments and original drawing of the paper)
Golang string (string) and byte array ([]byte) are converted to each other
How does GCN use large convolution instead of small convolution? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)
Common PostgreSQL commands