902. Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.
You may assume k is always valid, 1 ≤ k ≤ BST's total elements.
样例
Given root = {1,2}, k = 2, return 2.
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param root: the given BST
* @param k: the given k
* @return: the kth smallest element in BST
*/
//迭代遍历到最后(思考如何提前终止)
int count = 0;
int res;
int kthSmallest(TreeNode * root, int k) {
// write your code here
if (root->left != NULL) kthSmallest(root->left, k);
if (++count == k) res = root->val; //①
if (root->right != NULL) kthSmallest(root->right, k);
return res;
}
//循环
int kthSmallest(TreeNode * root, int k) {
// write your code here
std::stack<TreeNode*> sta;
while (true) {
while (root) {
sta.push(root);
root = root->left;
}
if (sta.empty()) break;
root = sta.top();
sta.pop();
if(--k==0) return root->val;
root = root->right;
}
}
};
902. Kth Smallest Element in a BST的更多相关文章
- 【LeetCode】230. Kth Smallest Element in a BST (2 solutions)
Kth Smallest Element in a BST Given a binary search tree, write a function kthSmallest to find the k ...
- [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素
题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...
- LeetCode 230. 二叉搜索树中第K小的元素(Kth Smallest Element in a BST)
230. 二叉搜索树中第K小的元素 230. Kth Smallest Element in a BST 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的 ...
- 【刷题-LeetCode】230. 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 ...
- [LeetCode] Kth Smallest Element in a BST 二叉搜索树中的第K小的元素
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- Leetcode 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
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- Kth Smallest Element in a 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 ——迭代本质: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 ...
随机推荐
- Teradata 终止回滚方法(rcvmanager工具)
1.使用root用户登录数据库节点 ssh root 2.启动database window cnsterm 3.启动rcvmanager start rcvmanager 4.确认utiltiy在哪 ...
- mabatis insert into on duplicate key
一.mabatis实现saveOrUpdate功能 <insert id="insert" parameterType="hystrixconfigdo" ...
- OLTP与OLAP的区别
OLTP和OLAP的区别 联机事务处理OLTP(on-line transaction processing) 主要是执行基本日常的事务处理,比如数据库记录的增删查改.比如在银行的一笔交易记录,就是一 ...
- 笔记-Android中打开各种格式的文件(apk、word、excel、ppt、pdf、音视频、图片等)
打开后缀.apk的文件.即启动安装程序. //apkFilePath 文件路径 public void installAPK(String apkFilePath) { // 创建URI Uri ur ...
- 洛谷P1274-魔术数字游戏
Problem 洛谷P1274-魔术数字游戏 Accept: 118 Submit: 243Time Limit: 1000 mSec Memory Limit : 128MB Probl ...
- 【转】ffmpeg 常用命令
1. 视频转换 比如一个avi文件,想转为mp4,或者一个mp4想转为ts. ffmpeg -i input.avi output.mp4 ffmpeg -i input.mp4 output.ts ...
- 测试 Flask 应用
测试 Flask 应用 没有经过测试的东西都是不完整的 这一箴言的起源已经不可考了,尽管他不是完全正确的,但是仍然离真理不远.没有测试过的应用将会使得提高现有代码质量很困难,二不测试应用程序的开发者, ...
- ActiveMQ发布订阅模式 转发 https://www.cnblogs.com/madyina/p/4127144.html
ActiveMQ的另一种模式就SUB/HUB即发布订阅模式,是SUB/hub就是一拖N的USB分线器的意思.意思就是一个来源分到N个出口.还是上节的例子,当一个订单产生后,后台N个系统需要联动,但有一 ...
- jmeter(二十二)内存溢出原因及解决方法
jmeter是一个java开发的开源性能测试工具,在性能测试中可支持模拟并发压测,但有时候当模拟并发请求较大或者脚本运行时间较长时,压力机会出现卡顿甚至报异常————内存溢出, 这里就介绍下如何解决内 ...
- Burp Suite学习之Intruder的4种攻击模式
burp suit的intruder攻击共有四种模式,如图所示,下面分别讲讲这四种模式的使用方法和场景. 一 .Sniper模式 Sniper模式使用一组payload集合,它一次只使用一个paylo ...