当前位置:网站首页>剑指 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
边栏推荐
- 单点登录(一处登录,处处可用)
- VPP cannot load up status interface
- 微服务 - 配置中心 - Nacos
- 线性代数(三)
- Get URL of [url reference]? For the following parameters, there are two ways to get the value of the corresponding parameter name and convert the full quantity to the object structure
- PostgreSQL learning 04 PG_ hint_ Plan installation and use, SQL optimization knowledge
- R language uses wilcox.test function to perform Wilcox signed rank test to obtain confidence interval of population median (set conf.level parameter to specify confidence level and size of confidence
- Three billion dollars! Horizon becomes the world's highest valued AI chip Unicorn
- Leetcode 0121. the best time to buy and sell stocks - simulation from back to front
- LCP plug-in creates peer VLAN interface
猜你喜欢

The selection reference of Junzheng T41, T40 and T31 versions are all here

New discovery of ROS callback function

Programming hodgepodge (I)

Odoo14 | about the abnormal display of statusbar keyword after use and Its Solutions

Unity接入ChartAndGraph图表插件

LeetCode 15:三数之和

Realsense d435i depth map optimization_ High precision mode

sqlilabs less-28~less-8a

50 places are limited to open | with the news of oceanbase's annual press conference coming!

Talk about how redis handles requests
随机推荐
background
Dynamic planning learning notes
Automatic usage in SystemVerilog
ECS is exclusive to old users, and the new purchase of the remaining 10 instances is as low as 3.6% off
Differences and application directions of GPS, base station and IP positioning
2020icpc Jiangxi warm up e.robot sends red packets (DFS)
Adaptation dynamics | in June, sequoiadb completed mutual certification with five products
obj文件格式与.mtl文件格式
R language uses wilcox.test function to perform Wilcox signed rank test to obtain confidence interval of population median (set conf.level parameter to specify confidence level and size of confidence
Leetcode 237. 删除链表中的节点
同条网线电脑正常上网,手机连接wifi成功,但是无法访问互联网
Programming hodgepodge (I)
Concepts of phase velocity and phase in transmission line theory
Airserver 7.3.0 Chinese version mobile device wireless transmission computer screen tool
The difference between $write and $display in SystemVerilog
Equal proportion of R language test group: use the prop.test function to test whether the success proportion of the two groups is equal
Detailed explanation of stepn chain game system development mode (Sports money making mode)
Working principle and precautions of bubble water level gauge
剑指 Offer 05. 替换空格
Tips for downloading videos in batches