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.
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?
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
stack <TreeNode*> tmp_s;
TreeNode* tmp=root;
while(tmp!=NULL){
tmp_s.push(tmp);
tmp=tmp->left;
}
while(k>&&(tmp!=NULL||!tmp_s.empty())){
if(tmp==NULL){
tmp=tmp_s.top();
tmp_s.pop();
if(--k==)
return tmp->val;
tmp=tmp->right;
}
else{
while(tmp!=NULL){
tmp_s.push(tmp);
tmp=tmp->left;
}
}
}
return tmp->val;
}
};
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 ...
- 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 ...
- 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 ...
随机推荐
- 我的android学习经历
我为什么选择android? 我基本上前一年的时间都是在学习java的语法和线程之类的,没有注意java的分类,所以到现在慢慢接触到深处的时候我了解到,java的优势主要在web,而我不是特别喜欢网页 ...
- Selenium Test 自动化测试 入门级学习笔记
1.下载安装Firefox-selenium插件 需要下载插件可以联系,这里暂不提供下载地址. 2.集成Eclipse 需要下载jar包可以联系,这里暂不提供下载地址. 集成Eclipse非常简单,加 ...
- GitHub Desktop 桌面工具,离线版本下载(无需考虑网络问题)
http://pan.baidu.com/s/1qYq4X0C GitHub Desktop 桌面工具,离线版本下载 对于网络不好,不稳定,安装多次都不成功的,这是你们的最好的安装方法了.
- VS2015 Git 插件使用教程
VS2015 中继承了 Git 插件,再也不用下载 Github for Windows了. 从 团队-管理连接 中打开 团队资源管理器 克隆Repository 在 本地 Git 存储库下面点击 ...
- .NET文档生成工具ADB使用图文教程
.NETv3.0 可再发行组件包 类型:编程辅助大小:2.7M语言:中文 评分:1.8 标签: 立即下载 ADB2.3使用指南 ADB2.3使用方法如下图所示: (1)主界面: (2)批量选择: 4. ...
- druid连接池异常
在从excel导入10W条数据到mysql中时,运行一段时间就会抛这个异常,连接池问题 org.springframework.transaction.CannotCreateTransactionE ...
- Javascript之旅——第十站:为什么都说闭包难理解呢?
研究过js的朋友大多会说,理解了js的原型和闭包就可以了,然后又说这些都是js的高级内容,然后就又扯到了各种神马的作用域...然后不少 人就会被忽悠的云里雾里...下面我也试着来说说闭包,看我说的这个 ...
- RabbitMq 技术文档
RabbitMq 技术文档 目录 1 AMQP简介 2 AMQP的实现 3 RabbitMQ简介 3.1 概念说明 3.2 消息队列的使用过程 3.3 RabbitMQ的特性 4 RabbitMQ使用 ...
- spring annotation简述
一.Annotation基本概念 Annotation是jdk5以后出现的新特性,在jdk中,其内置了许多自己的Annotation,例如@Override,@SuppresWarning,@Depr ...
- 0008《SQL必知必会》笔记04-子查询、联接与组合查询
1.子查询:就是嵌套的查询,用一个查询的结果作为另一个查询的条件. 比如要列出订购了物品“RGAN01”的所有顾客的ID.姓名和联系人,需要经历以下几步 (1)从orderitems中找出订购了“RG ...