当前位置:网站首页>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模板
边栏推荐
- FBI warning: some people use AI to disguise themselves as others for remote interview
- leetcode:11. 盛最多水的容器【雙指針 + 貪心 + 去除最短板】
- 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
- 198. Looting - Dynamic Planning
- php-fpm的max_chindren的一些误区
- Zhengda futures news: soaring oil prices may continue to push up global inflation
- After nohup NPM start &, close the shell window directly, and the process closes accordingly
- 2022-2028 global aircraft head up display (HUD) industry research and trend analysis report
- Change the single node of Postgres database into master-slave
- How to disable the clear button of ie10 insert text box- How can I disable the clear button that IE10 inserts into textboxes?
猜你喜欢

The vscode code is automatically modified to a compliance code when it is formatted and saved

Real time split network (continuous update)

English语法_名词 - 分类

English grammar_ Noun classification

2022-2028 global petroleum pipe joint industry research and trend analysis report

Computer graduation design PHP sports goods online sales system website

Add control at the top of compose lazycolumn

2022-2028 global plasmid DNA cdmo industry research and trend analysis report

2022.02.11
![[Yu Yue education] theoretical mechanics reference materials of Shanghai Jiaotong University](/img/52/b97c618a8f2eb29ad0ccca221bb5c1.jpg)
[Yu Yue education] theoretical mechanics reference materials of Shanghai Jiaotong University
随机推荐
Torch learning notes (7) -- take lenet as an example for dataload operation (detailed explanation + reserve knowledge supplement)
[combinatorics] exponential generating function (example 2 of solving multiple set permutation with exponential generating function)
Getting started with JDBC
[combinatorics] generating function (use generating function to solve the number of solutions of indefinite equation example 2 | extended to integer solution)
Computer graduation design PHP sports goods online sales system website
How to expand the capacity of golang slice slice
[combinatorics] exponential generating function (concept of exponential generating function | permutation number exponential generating function = combinatorial number ordinary generating function | e
How to analyze the rising and falling rules of London gold trend chart
English語法_名詞 - 分類
多媒体NFT聚合平台OKALEIDO即将上线,全新的NFT时代或将来临
SQL injection -day16
Raft log replication
2022-2028 global aircraft head up display (HUD) industry research and trend analysis report
204. Count prime
English语法_名词 - 分类
English语法_形容词/副词3级 - 倍数表达
Coordinate layer conversion tool (video)
There are several levels of personal income tax
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
虚拟机和开发板互Ping问题