当前位置:网站首页>Sword finger offer 54. the k-th node of the binary search tree
Sword finger offer 54. the k-th node of the binary search tree
2022-07-25 05:46:00 【The harder you work, the luckier you are】
In the sequence traversal by “ Left 、 root 、 Right ” The order , The recursive method code is as follows :
# Print sequence traversal
def dfs(root):
if not root: return
dfs(root.left) # Left
print(root.val) # root
dfs(root.right) # Right
The first method , Middle order ergodic binary tree , Output the last of k individual
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]The solution in this paper is based on this property : The middle order traversal of binary search tree is Increasing sequence .
According to the above properties , Easy to get binary search tree The middle order traverses the reverse order by Decreasing sequence .
therefore , seek “ Binary search tree No k Big nodes ” It can be transformed into seeking “ The middle order of this tree traverses the reverse order k Nodes ”
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 resIf , I don't want to add nonlocal, Then set the variable to self.xxx In the form of , This is equivalent to a global variable , Can be used anywhere
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
边栏推荐
- PostgreSQL learning 04 PG_ hint_ Plan installation and use, SQL optimization knowledge
- Leetcode 0121. the best time to buy and sell stocks - simulation from back to front
- (Niuke multi School II) j-link with arithmetic progress (least square method / three points)
- (16)[系统调用]追踪系统调用(3环)
- Msys2 common configuration
- HTB-Granpa
- Dynamic planning learning notes
- 50 places are limited to open | with the news of oceanbase's annual press conference coming!
- Programming hodgepodge (I)
- Run length test of R language: use the runs.test function to perform run length test on binary sequence data (check whether the sequence is random)
猜你喜欢

Leetcode 202. happy number (not happy at all)

HTB-Devel

Softing pngate series gateway: integrate PROFIBUS bus into PROFINET network

Working principle and precautions of bubble water level gauge

Realsense D435i 深度图优化_高精度模式

Softing pnGate系列网关:将PROFIBUS总线集成到PROFINET网络

CCID released the "Lake warehouse integrated technology research report", and Jushan database was selected as a typical representative of domestic enterprises

What are the ways to realize web digital visualization?

50:第五章:开发admin管理服务:3:开发【查询admin用户名是否已存在,接口】;(这个接口需要登录时才能调用;所以我们编写了拦截器,让其拦截请求,判断用户是否是登录状态;)

Amazoncaptcha 95%成功率绕过亚马逊IP验证码
随机推荐
(2022 Niuke multi School II) k-link with bracket sequence I (dynamic planning)
An SQL execution process
leetcode/二进制加法
The selection reference of Junzheng T41, T40 and T31 versions are all here
Leetcode 0122. the best time to buy and sell stocks II
Promise implementation
VIM configuring golang development environment
Leetcode 202. happy number (not happy at all)
Leetcode 237. 删除链表中的节点
2020ICPC 江西省赛热身赛 E.Robot Sends Red Packets(dfs)
Flexible layout summary
Talk about how redis handles requests
(2022年牛客多校一)I-Chiitoitsu(期望DP)
After Oracle user a deletes a table under user B's name, can user B recover the deleted table through the recycle bin?
Run length test of R language: use the runs.test function to perform run length test on binary sequence data (check whether the sequence is random)
PHP warehouse inventory management system source code WMS source code
context must be a dict rather解决
Why is it that all the games are pseudorandom and can't make true random?
Automatic usage in SystemVerilog
sqlilabs less-29
https://leetcode.cn/problems/er-cha-sou-suo-shu-de-di-kda-jie-dian-lcof/