230. Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.
Note:
You may assume k is always valid, 1 ≤ k ≤ BST's total elements.
Example 1:
Input: root = [3,1,4,null,2], k = 13/ \1 4\2
Output: 1
Example 2:
Input: root = [5,3,6,2,4,null,null,1], k = 35/ \3 6/ \2 4/1
Output: 3
Follow up:
What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?
难度:medium
题目:给定二叉搜索树,找出其值第K小的结点。
思路:中序遍历
Runtime: 0 ms, faster than 100.00% of Java online submissions for Kth Smallest Element in a BST.
Memory Usage: 38.9 MB, less than 19.71% of Java online submissions for Kth Smallest Element in a BST.
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/
public class Solution {public int kthSmallest(TreeNode root, int k) {int[] result = {root.val};kthSmallest(root, k, new int[1], result);return result[0];}public void kthSmallest(TreeNode root, int k, int[] count, int[] result) {if (root == null || count[0] >= k) {return;}kthSmallest(root.left, k, count, result);count[0]++;if (count[0] == k) {result[0] = root.val;return;} kthSmallest(root.right, k, count, result);}
}
230. Kth Smallest Element in a BST相关推荐
- [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素
题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...
- 230. Kth Smallest Element in a BST ——迭代本质:a=xx1 while some_condition: a=xx2
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- Leetcode - 230. Kth Smallest Element in a BST (BST)
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- 230. Kth Smallest Element in a BST
题目: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. ...
- LeetCode 230. Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- leetcode 230. Kth Smallest Element in a BST | 230. 二叉搜索树中第K小的元素(Java)
题目 https://leetcode.com/problems/kth-smallest-element-in-a-bst/ 题解 方法1:中序遍历+剪枝 import java.util.Arra ...
- [LeetCode] 230. Kth Smallest Element in a BST
题目内容 https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst/ 给定一个二叉搜索树,编写一个函数 kthSmallest 来查 ...
- LeetCode 230. Kth Smallest Element in a BST--C++,Python解法--面试真题--找二叉树中第K小的元素
题目地址:Kth Smallest Element in a BST - LeetCode Given a binary search tree, write a function kthSmalle ...
- [LeetCode] Kth Smallest Element in a BST
Kth Smallest Element in a BST Given a binary search tree, write a function kthSmallest to find the k ...
最新文章
- 装上螺旋桨,加州理工让只能行走的双足机器人「上了天」,还玩起了障碍滑板、走绳索...
- Flutter中关键字Const和Final之间的区别
- day05 Spring中自定义注解的用处-之获取自定义的Servie
- 0x80070659系统策略禁止这个安装 vc_教你一招,解决所有系统丢失DLL文件的问题!...
- ASP.NET Web API 特性
- Linux下gcc入门
- 设计模式之十(外观模式)
- 前端学习(2343):理解virtaldom和key
- 【蓝桥杯官网训练 - 历届试题】对局匹配(dp,思维,取模)
- 面积积分_袁颖妍:用定理积分求平面区域面积(有代表性的9个例题)
- go Mutex (互斥锁)和RWMutex(读写锁)
- [Bzoj4817] [Sdoi2017]树点涂色 (LCT神题)
- JavaScript语言核心(五)-- 异步 async/await
- 计算机专业论文答辩ppt,计算机专业毕业答辩ppt模板
- 重庆要做的“边缘计算”,是什么?
- 点桌面计算机一直在加载,Win7系统在桌面点右键一直转圈的解决方法
- BIOS INT中断整理
- 微信斑马系统:微信朋友圈广告背后的利器
- Zmeet云雾架构-融合AI能力的通讯层的变革,性能远超传统通讯技术
- 利用python的docx模块处理word和WPS的docx格式文件
热门文章
- DSP关于存储器读写、IO读写时序图的注意点
- PHP checkdate()函数与示例
- 给定条件找最小值c语言程序_根据给定条件最小化n的最小步骤
- Java——网络编程三要素
- 【操作系统】互斥:软件解决方法
- 伪静态设置 html,Apache下伪静态html(URL Rewrite)的设置方法
- python3 Crypto使用出现的问题
- input数字开头不能为0_李商隐为初恋写诗,每句以数字开头,最后10字一直被仿从未被超越...
- Linux中变量#,@,0,1,2,*,$$,$?的含义
- 线程间通信————互斥