当前位置:网站首页>Leetcode -- the kth largest node of the binary search tree (traversal by means of middle order)
Leetcode -- the kth largest node of the binary search tree (traversal by means of middle order)
2022-06-22 04:48:00 【Always--Learning】
Title Description

Their thinking
First of all, we need to know what is a binary search tree , The characteristic of binary search tree is that the left child node of a node is smaller than the node , The right child node is larger than this node .
So what are the characteristics of the middle order traversal of binary search trees ?
Binary search tree : [3,1,4,null,2]
Middle order traversal result :[1,2,3,4] The first 1 The big element is res.length - 1(k): Subscript to be 3
Binary search tree :[5,3,6,2,4,null,null,1]
Middle order traversal result :[1,2,3,4,5,6] The first 3 The big element is res.length - 3(k): Subscript to be 4
From the above example, we can see that , To find the second of a binary search tree K Big node , A binary search tree can be traversed in middle order , Then subtract the length of the traversal result array K That is the first. K Subscript corresponding to a large node .
- If the incoming node is empty , Then return to null.
if (!root) return null;
- Define result array
const res = [];
- Do middle order traversal
const midOrder = (root) => {
if (!root) return null;
midOrder(root.left);
res.push(root.val);
midOrder(root.right);
}
midOrder(root);
- Return to binary search tree No K Subscript corresponding to a large node
return res[res.length - k];
AC Code
var kthLargest = function(root, k) {
if (!root) return null;
const res = [];
const midOrder = (root) => {
if (!root) return null;
midOrder(root.left);
res.push(root.val);
midOrder(root.right);
}
midOrder(root);
return res[res.length - k];
};
reflection
The second of binary search tree K Large nodes are a good topic , I originally wanted to traverse the binary search tree directly , And then sort it , Then go back to K Big nodes , But this obviously does not take advantage of the characteristics of binary search tree , In particular, it does not use the binary search tree to traverse the middle order , As long as we know the characteristics of middle order traversal of binary search tree , It can be easily solved .
边栏推荐
- NLP 的 不可能三角?
- mongo模糊查询,带有特殊字符需要转义,再去查询
- Cloud function realizes fuzzy search function
- C # WinForm listview control has no scroll bar and has written an extension control by itself
- 招贤纳士-第23期
- Golang為什麼不推薦使用this/self/me/that/_this
- Knowledge points used in MVC project development (mvccontrib separates asp.net MVC project)
- Take you to develop an efficiency enhancing tool -- vscode plug-in
- mysql笔记
- Learning signal integrity from scratch -- 7-si analysis and simulation
猜你喜欢

It is easy to analyze and improve R & D efficiency by understanding these five figures

网页设计与制作期末大作业报告——动画家宫崎骏

解决Swagger2显示UI界面但是没内容

The application of RPC technology and its framework sekiro in crawler reverse, encrypting data is a shuttle!

Qt项目的新首席维护人员

《数据库原理》期末考试题

About SSM integration, this is enough ~ (nanny level hands-on tutorial)

What is a forum virtual host? How to choose?

Ora - 15063: ASM discovered an insufficient number of Disks for diskgroup Recovery - - -

UC San Diego | evit: using token recombination to accelerate visual transformer (iclr2022)
随机推荐
当程序员编程时被打扰 | 每日趣闻
The application of RPC technology and its framework sekiro in crawler reverse, encrypting data is a shuttle!
Arrangement of soft test subjects in the second half of 2022
Calculation of audio frame size
ORA-15063: ASM discovered an insufficient number of disks for diskgroup 恢複---惜分飛
Odoo Development Manual (I) the second day of contact with odoo
Graph calculation on nlive:nepal's graph calculation practice
浏览器--常用的搜索操作符大全--使用/实例
Circuit board layout and wiring precautions for analog / digital mixed signals
After the active RM machine is powered off, RM ha switching is normal. However, the cluster resources cannot be viewed on the yarnui, and the application is always in the accepted state.
Online document collaboration: a necessary efficient artifact for office
It is easy to analyze and improve R & D efficiency by understanding these five figures
Requests cookie update value
源代码加密技术科普
【SDX62】IPA log抓取操作说明
Interaction between C language and Lua (practice 3)
Kotlin项目报错缺少CoroutineContext依赖
厉害了!淮北两企业获准使用地理标志产品专用标志
The best time to climb a ladder & sell shares (notes of the runner)
[从零开始学习FPGA编程-39]:进阶篇 - 语法-硬件模块的单元测试:仿真激励、testbench