当前位置:网站首页>剑指 Offer 54. 二叉搜索树的第k大节点
剑指 Offer 54. 二叉搜索树的第k大节点
2022-07-25 05:41:00 【愈努力俞幸运】
剑指 Offer 54. 二叉搜索树的第k大节点
https://leetcode.cn/problems/er-cha-sou-suo-shu-de-di-kda-jie-dian-lcof/
中序遍历 为 “左、根、右” 顺序,递归法代码如下:
# 打印中序遍历
def dfs(root):
if not root: return
dfs(root.left) # 左
print(root.val) # 根
dfs(root.right) # 右
第一种方法,中序遍历二叉树,输出倒数第k个
class Solution:
def kthLargest(self, root, k):
lst=[]
def dfs(cur):
if not cur: return
dfs(cur.left)
lst.append(cur.val)
dfs(cur.right)
dfs(root)
return lst[len(lst)-k]本文解法基于此性质:二叉搜索树的中序遍历为 递增序列 。
根据以上性质,易得二叉搜索树的 中序遍历倒序 为 递减序列 。
因此,求 “二叉搜索树第 k大的节点” 可转化为求 “此树的中序遍历倒序的第 k个节点”
class Solution:
def kthLargest(self, root, k):
count=k
res=0
def dfs(cur):
nonlocal count
nonlocal res
if not cur: return
dfs(cur.right)
if count==0:return
count-=1
if count==0: res=cur.val
dfs(cur.left)
dfs(root)
return res如果,不想加nonlocal,则就把变量设置成self.xxx的形式,此时相当于全局变量,哪里都可以使用
class Solution:
def kthLargest(self, root, k):
def dfs(cur):
if not cur: return
dfs(cur.right)
if self.count==0:return
self.count-=1
if self.count==0:self.res=cur.val
dfs(cur.left)
self.count=k
dfs(root)
return self.res
边栏推荐
- Leetcode 0122. the best time to buy and sell stocks II
- 传输线理论之相速、相位等的概念
- 计算BDP值和wnd值
- 对于von Mises distribution(冯·米塞斯分布)的一点心得
- 新时代生产力工具——FlowUs 息流全方位评测
- 微服务 - 网关Gateway组件
- Switch NPM source to private source library
- HTB-Optimum
- Working principle and precautions of bubble water level gauge
- R language obtains the data row where the nth maximum value of the specified data column is located in the data.table data
猜你喜欢

HTB-Granpa

Productivity tool in the new era -- flowus information flow comprehensive evaluation

Concepts of phase velocity and phase in transmission line theory

Vim查找替换及正则表达式的使用

线性代数(三)

Application of hard coding and streaming integration scheme based on spice protocol in cloud games

编程大杂烩(二)

Leetcode 202. 快乐数(一点都不快乐)

Leetcode 204. 计数质数(太妙了)

The computer accesses the Internet normally with the same network cable, and the mobile phone connects to WiFi successfully, but it cannot access the Internet
随机推荐
uniapp手机端uView的u-collapse组件高度init
Arm PWN basic tutorial
Typera+picgo+ Alibaba cloud OSS setup and error reporting solution [reprint]
R language data The table package performs aggregation transforms of data packets and calculates the grouping interquartile range (IQR) of dataframe data
微服务 - 网关Gateway组件
HTB-Granpa
Working principle and precautions of bubble water level gauge
线性代数(三)
Talk about how redis handles requests
Leetcode 204. 计数质数(太妙了)
The selection reference of Junzheng T41, T40 and T31 versions are all here
PMP Exam is easy to confuse concept discrimination skills! Don't lose points after reading!
Basset: learning the regulatory code of the accessible genome with deep convolutional neural network
Base64 (conversion between string and Base64 string)
C Programming -- the solution of dynamic programming of "the sum of the largest subarray"
Equal proportion of R language test group: use the prop.test function to test whether the success proportion of the two groups is equal
同条网线电脑正常上网,手机连接wifi成功,但是无法访问互联网
Idea commonly used 10 shortcut keys
Exchange 2010 SSL certificate installation document
Openfegin remote call lost request header problem