当前位置:网站首页>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模板
边栏推荐
- Common PostgreSQL commands
- [combinatorics] exponential generating function (example of exponential generating function solving multiple set arrangement)
- SQL injection -day16
- VLAN experiment
- Win 11 major updates, new features love love.
- Zero length array
- How to disable the clear button of ie10 insert text box- How can I disable the clear button that IE10 inserts into textboxes?
- Install apache+php+mysql+phpmyadmin xampp and its error resolution
- 235. 二叉搜索树的最近公共祖先【lca模板 + 找路径相同】
- Administrative division code acquisition
猜你喜欢

English grammar_ Noun classification

Zhengda futures news: soaring oil prices may continue to push up global inflation

How many convolution methods does deep learning have? (including drawings)

How to quickly view the inheritance methods of existing models in torchvision?

FBI warning: some people use AI to disguise themselves as others for remote interview

2022-2028 global solid phase extraction column industry research and trend analysis report

论文阅读 GloDyNE Global Topology Preserving Dynamic Network Embedding

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

Install apache+php+mysql+phpmyadmin xampp and its error resolution

Computer graduation project PHP library book borrowing management system
随机推荐
Torch learning notes (4) -- torch's dynamic calculation diagram
What kind of experience is it when the Institute earns 20000 yuan a month?
Sensor debugging process
2022-2028 global scar care product industry research and trend analysis report
Opencv learning notes (continuously updated)
Typescript configuration
How to disable the clear button of ie10 insert text box- How can I disable the clear button that IE10 inserts into textboxes?
How to expand the capacity of golang slice slice
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
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
Torch learning notes (7) -- take lenet as an example for dataload operation (detailed explanation + reserve knowledge supplement)
An academic paper sharing and approval system based on PHP for computer graduation design
[combinatorics] generating function (positive integer splitting | unordered | ordered | allowed repetition | not allowed repetition | unordered not repeated splitting | unordered repeated splitting)
Golang string (string) and byte array ([]byte) are converted to each other
Zero length array
leetcode:556. Next larger element III [simulation + change as little as possible]
论文阅读 GloDyNE Global Topology Preserving Dynamic Network Embedding
2022-2028 global petroleum pipe joint industry research and trend analysis report
Torch learning notes (5) -- autograd
Install apache+php+mysql+phpmyadmin xampp and its error resolution