当前位置:网站首页>235. 二叉搜索树的最近公共祖先【lca模板 + 找路径相同】
235. 二叉搜索树的最近公共祖先【lca模板 + 找路径相同】
2022-07-03 18:38: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] exponential generating function (proving that the exponential generating function solves the arrangement of multiple sets)
- Data analysis is popular on the Internet, and the full version of "Introduction to data science" is free to download
- 2022-2028 global scar care product industry research and trend analysis report
- How to disable the clear button of ie10 insert text box- How can I disable the clear button that IE10 inserts into textboxes?
- Theoretical description of linear equations and summary of methods for solving linear equations by eigen
- Recommend a simple browser tab
- Computer graduation design PHP makeup sales Beauty shopping mall
- [Yu Yue education] theoretical mechanics reference materials of Shanghai Jiaotong University
- English语法_名词 - 分类
- How about the Moco model?
猜你喜欢
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
2022-2028 global plasmid DNA cdmo industry research and trend analysis report
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
Read the paper glodyne global topology preserving dynamic network embedding
SQL injection -day16
Okaleido, a multimedia NFT aggregation platform, is about to go online, and a new NFT era may come
What problems can cross-border e-commerce sellers solve with multi platform ERP management system
Multifunctional web file manager filestash
English語法_名詞 - 分類
Real time split network (continuous update)
随机推荐
Read the paper glodyne global topology preserving dynamic network embedding
2022-2028 global scar care product industry research and trend analysis report
12、 Service management
Torch learning notes (3) -- univariate linear regression model (self training)
[combinatorics] generating function (positive integer splitting | unordered | ordered | allowed repetition | not allowed repetition | unordered not repeated splitting | unordered repeated splitting)
多媒体NFT聚合平台OKALEIDO即将上线,全新的NFT时代或将来临
Torch learning notes (2) -- 11 common operation modes of tensor
Torch learning notes (5) -- autograd
[combinatorics] exponential generating function (proving that the exponential generating function solves the arrangement of multiple sets)
Golang string (string) and byte array ([]byte) are converted to each other
Raft log replication
041. (2.10) talk about manpower outsourcing
论文阅读 GloDyNE Global Topology Preserving Dynamic Network Embedding
Database creation, addition, deletion, modification and query
[combinatorics] generating function (example of using generating function to solve the number of solutions of indefinite equation)
Shell script return value with which output
知其然,而知其所以然,JS 对象创建与继承【汇总梳理】
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
Computer graduation design PHP makeup sales Beauty shopping mall
Grammaire anglaise Nom - Classification