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 ...
随机推荐
- 数据结构实验之排序一:一趟快排( SDUT 3398)
#include <stdio.h> #include <string.h> int a[110000]; void qusort(int l, int r, int a[]) ...
- python 中 list 去重复
方法1 list=[,,,] set=set(list) list2=list(set) 方法2 list=[,,,] list2=[] for i in list: if i not in list ...
- jquery中$.get()如何让跨域请求携带cookie?
在这个get请求前面加上这个就好了~~~~
- 普通的checkbox的回显功能
var ypxt=document.getElementsByName("map.LCSYLB"); var jgjg='${map.LCSYLB}'; ...
- 2018-2019-2 《网络对抗技术》Exp9 WebGoat 20165326
Web安全基础 jar包,密码:9huw 实验问题回答 SQL注入攻击原理,如何防御 原理:恶意用户在提交查询请求的过程中将SQL语句插入到请求内容中,同时程序本身对未对插入的SQL语句进行过滤,导致 ...
- Tomcat启动过程中找不到JAVA_HOME JRE_HOME的解决方法
转自:http://blog.sina.com.cn/s/blog_61c006ea0100l1u6.html 原文: 在XP上明明已经安装了JDK1.5并设置好了JAVA_HOME,可偏偏Tomca ...
- Guided Hacking DLL Injector 3.3
Guided Hacking DLL Injector 3.3 https://guidedhacking.com/resources/guided-hacking-dll-injector.4/ I ...
- 制作 python解释器
https://www.zhihu.com/tardis/sogou/qus/27286136
- python 设计模式之单例模式 Singleton Pattern
#引入 一个类被设计出来,就意味着它具有某种行为(方法),属性(成员变量).一般情况下,当我们想使用这个类时,会使用new 关键字,这时候jvm会帮我们构造一个该类的实例.这么做会比较耗费资源. 如果 ...
- layuiadmin(iframe)常用问题集锦
1. 弹出窗口中传值给父层表单 table.on('tool(tableList)', function(obj){ var selected = obj.data; var layEvent = o ...