LeetCode 230. 二叉搜索树中第K小的元素(Kth Smallest Element in a BST)
230. 二叉搜索树中第K小的元素
230. Kth Smallest Element in a BST
题目描述
给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素。
说明:
你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数。
LeetCode230. Kth Smallest Element in a BST
示例 1:
```
3
/ \
1 4
\
2
```
输出: 1
示例 2:
```
5
/ \
3 6
/ \
2 4
/
1
```
输出: 3
进阶:
如果二叉搜索树经常被修改(插入/删除操作)并且你需要频繁地查找第 k 小的值,你将如何优化 kthSmallest 函数?
Java 实现
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
class Solution {
public int kthSmallest(TreeNode root, int k) {
int count = countNodes(root.left);
if (k <= count) {
return kthSmallest(root.left, k);
} else if (k > count + 1) {
return kthSmallest(root.right, k - 1 - count);
}
return root.val;
}
public int countNodes(TreeNode n) {
if (n == null) {
return 0;
}
return 1 + countNodes(n.left) + countNodes(n.right);
}
}
相似题目
参考资料
- https://leetcode.com/problems/kth-smallest-element-in-a-bst/
- https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst/
LeetCode 230. 二叉搜索树中第K小的元素(Kth Smallest Element in a BST)的更多相关文章
- 【LeetCode】230. 二叉搜索树中第K小的元素 Kth Smallest Element in a BST
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:算法题,刷题,Leetcode, 力扣,二叉搜索树,BST ...
- [Swift]LeetCode230. 二叉搜索树中第K小的元素 | Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- Java实现 LeetCode 230 二叉搜索树中第K小的元素
230. 二叉搜索树中第K小的元素 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明: 你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. ...
- [LeetCode]230. 二叉搜索树中第K小的元素(BST)(中序遍历)、530. 二叉搜索树的最小绝对差(BST)(中序遍历)
题目230. 二叉搜索树中第K小的元素 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 题解 中序遍历BST,得到有序序列,返回有序序列的k-1号元素. 代 ...
- LeetCode——230. 二叉搜索树中第K小的元素
给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明: 你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. 示例 1: 输入: root = ...
- leetcode 230 二叉搜索树中第K小的元素
方法1:统计每个节点的子节点数目,当k>左子树节点数目时向左子树搜索,k=左子树节点数目时返回根节点,否则向右子树搜索. 方法2:递归中序遍历,这里开了O(n)空间的数组. class Solu ...
- LeetCode 230. 二叉搜索树中第K小的元素(Kth Smallest Element in a BST)
题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. 示例 1: 输入: roo ...
- leetcode 230. 二叉搜索树中第K小的元素(C++)
给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. 示例 1: 输入: root = [ ...
- leetcode 230二叉搜索树中第k小的元素
通过stack进行中序遍历迭代,timeO(k),spaceO(1) /** * Definition for a binary tree node. * struct TreeNode { * in ...
随机推荐
- linux 查看带宽瓶颈
1.首先要确定网卡带宽是多少(单位是Mbit/s) ethtool eth1 | grep Speed 2.确定当前带宽使用情况 使用 nload 工具,如果没有可以yum install nload ...
- springcloud config自动刷新中文乱码问题
摘录一段网上盛传的,如下: 在使用 spring cloud config 时,如果在 properties 文件里面有中文的话,会出现乱码. 乱码的原因是:spring 默认使用org.spring ...
- JavaScript data types and data structures
JavaScript data types and data structures Programming languages all have built-in data structures, b ...
- nmcli 配置ip
配置ip: nmcli connection modify 'Wired connection 1' connection.autoconnect yes ipv4.method manual ipv ...
- Mysql修改数据文件默认目录datadir
1.停止服务 service mysql stop service mysqld stopmysqladmin -uroot shutdown -p 2.创建目录 cd /data mkdir mys ...
- You are using the runtime-only build of Vue where the template compiler is not available. Either pre
在升级脚手架到vue-cli3.0版本的时候出现了这个报错: [Vue warn]: You are using the runtime-only build of Vue where the tem ...
- Data Governance Solution
如何有效地进行数据治理 | 人人都是产品经理http://www.woshipm.com/data-analysis/746223.html ##普元元数据管理(MetaCube)产品-白皮书.doc ...
- java中 try catch的妙用
程序开发中,格式转换的时候,经常由于字符串可能是其他的不可预知的符号导致,字符串转数值失败, 这个时候可以妙用try catch来解决,如下图所示.其实,很多其他不可预知的异常情况,也可以用它来处理. ...
- quasar 报错
yarn是项目建议安装的方式,报错如下 npm 安装的项目,报错如下 情况:无论是哪种安装方式,似乎报错都出在同一个地方. 近况,还在解决中...
- [Sklearn] Linear regression models to fit noisy data
Ref: [Link] sklearn各种回归和预测[各线性模型对噪声的反应] Ref: Linear Regression 实战[循序渐进思考过程] Ref: simple linear regre ...