当前位置:网站首页>LeetCode——二叉搜索树的第k大节点(借助中序遍历)
LeetCode——二叉搜索树的第k大节点(借助中序遍历)
2022-06-22 04:41:00 【Always--Learning】
题目描述

解题思路
这个题目我们首先要知道什么是二叉搜索树,二叉搜索树的特点是一个节点的左子节点比该节点小,右子节点比该节点大。
那么二叉搜索树的中序遍历有什么特点?
二叉搜索树: [3,1,4,null,2]
中序遍历结果:[1,2,3,4] 第1大的元素是res.length - 1(k):下标为3
二叉搜索树:[5,3,6,2,4,null,null,1]
中序遍历结果:[1,2,3,4,5,6] 第3大的元素是res.length - 3(k):下标为4
从上面的例子中我们可以看出,要想求一颗二叉搜索树的第K大节点,可以对一颗二叉搜索树进行中序遍历,然后将遍历结果数组的长度减去K就是第K大节点对应的下标。
- 如果传入节点为空,则返回null。
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);
- 返回二叉搜索树第K大节点对应的下标
return res[res.length - k];
AC代码
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];
};
反思
二叉搜索树的第K大节点是一个很好的题目,我原本想的是直接对二叉搜索树进行遍历,然后进行排序,然后返回第K大的节点,但是这样做显然是没有利用二叉搜索树的特点,尤其是没有利用二叉搜索树进行中序遍历的特点,只要知道了二叉搜索树的中序遍历的特点,便可迎刃而解。
边栏推荐
- 【故障诊断】cv2.imwrite无法写入图片,但程序就是不报错
- 【科研笔记】关于使用openslide切图的下采样倍数
- 【故障诊断】stitch.py脚本失效
- 浏览器--常用的搜索操作符大全--使用/实例
- 系统整理|这个模型开发前的重要步骤有多少童鞋忘记细心做好(实操)
- C # WinForm listview control has no scroll bar and has written an extension control by itself
- How to check the domain name for free through IP?
- Use echart to draw 3D pie chart, dashboard and battery diagram
- Golang为什么不推荐使用this/self/me/that/_this
- Windows10 cannot access LAN shared folder
猜你喜欢

When the move protocol beta is in progress, the ecological core equity Momo is divided

Odoo Development Manual (I) the second day of contact with odoo

Windows10 cannot access LAN shared folder

Postman document parameterization

cadence allegro 17. X conversion tool for downgrading to 16.6

套用这套模板,玩转周报、月报、年报更省事

Redis 主从复制

POSIX shared memory

The best time to climb a ladder & sell shares (notes of the runner)

Large website technology architecture | application server security defense
随机推荐
Large website technology architecture | application server security defense
Daily question: the difference between ArrayList and LinkedList
Fastadmin list multi image preview (zoom in preview, rotation) one line of code
【使用指南】使用公共的conda创建环境
ORA-15063: ASM discovered an insufficient number of disks for diskgroup 恢複---惜分飛
Graph calculation on nlive:nepal's graph calculation practice
What is the value of the FC2 new domain name? How to resolve to a website?
Tianyang technology - Bank of Ningbo interview question [Hangzhou multi tester] [Hangzhou multi tester \wang Sir]
Calculation of audio frame size
Accurate identification of bank card information - smart and fast card binding
网页设计与制作期末大作业报告——大学生线上花店
Why does golang not recommend this/self/me/that/_ this
Search (intensive training)
Segment tree & tree array template
UC San Diego | evit: using token recombination to accelerate visual transformer (iclr2022)
Interaction between C language and Lua (practice 3)
【故障诊断】stitch.py脚本失效
Systematic arrangement | how many children's shoes have forgotten to be done carefully before the model development (practical operation)
Customized plug-ins in Cordova project -- plug-in creation process
fc2新域名有什么价值?如何解析到网站?