【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 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?
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
解法一:递归中序遍历,必须全部遍历
/**
* 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) {
vector<int> ret;
inOrder(root, ret);
return ret[k-];
}
void inOrder(TreeNode* root, vector<int>& ret)
{
if(root)
{
inOrder(root->left, ret);
ret.push_back(root->val);
inOrder(root->right, ret);
}
}
};

解法二:迭代中序遍历,遍历到第k个元素停止
/**
* 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) {
vector<int> ret;
stack<TreeNode*> stk;
stk.push(root);
TreeNode* cur = root;
while(cur->left)
{
stk.push(cur->left);
cur = cur->left;
}
while(!stk.empty())
{
TreeNode* top = stk.top();
stk.pop();
ret.push_back(top->val);
if(ret.size() == k)
break;
if(top->right)
{
TreeNode* cur = top->right;
stk.push(cur);
while(cur->left)
{
stk.push(cur->left);
cur = cur->left;
}
}
}
return ret[k-];
}
};

【LeetCode】230. Kth Smallest Element in a BST (2 solutions)的更多相关文章
- 【LeetCode】230. Kth Smallest Element in a BST
Difficulty: Medium More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/kth-smallest- ...
- 【刷题-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】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)
[LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 【Leetcode】378. Kth Smallest Element in a Sorted Matrix
Question: Given a n x n matrix where each of the rows and columns are sorted in ascending order, fin ...
- 【leetcode】378. Kth Smallest Element in a Sorted Matrix(TOP k 问题)
Given an n x n matrix where each of the rows and columns is sorted in ascending order, return the kt ...
- LeetCode OJ 230. Kth Smallest Element in a BST
Total Accepted: 46445 Total Submissions: 122594 Difficulty: Medium Given a binary search tree, write ...
- 【LeetCode】215. Kth Largest Element in an Array (2 solutions)
Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is t ...
- [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. 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 ...
随机推荐
- 命令行打印文件树列表: tree
Linux & Mac 1.下载tree lib //mac brew install tree //centos yum install tree //ubuntu apt-get inst ...
- LSTM 文本情感分析/序列分类 Keras
LSTM 文本情感分析/序列分类 Keras 请参考 http://spaces.ac.cn/archives/3414/ neg.xls是这样的 pos.xls是这样的neg=pd.read_e ...
- 【R】自定义函数方法
- 【Python】无须numpy,利用map函数与zip(*)函数对数组转置(转)
http://blog.csdn.net/yongh701/article/details/50283689 在Python的numpy中,对类似array=[[1,2,3],[4,5,6],[7,8 ...
- Android面试题收集
Android是一种基于Linux的自由及开放源码的操作系统,主要使用于移动设备.如智能手机和平板电脑.由Google公司和开放手机联盟领导及开发.这里会不断收集和更新Android基础相关的面试题, ...
- 放入MP3的文件夹显示一些没用的标题,艺术家,唱片集怎么办?
原来的文件大小,类型,修改日期,创建日线都没了,怎么办? 右键点击名称,标题,艺术家那一行,在右键菜单中,去掉没用的,勾选有用的,如下图:
- 删除数据库mysql
drop命令用于删除数据库. drop命令格式:drop database <数据库名>; 例如,删除名为 xhkdb的数据库:mysql> drop database xhkdb; ...
- ios Url Encode
//ios Url Encode //有时候在请求的参数里里特殊符号比如“+”等.而如果没有encode的话那么传过去的还是” ”,面实际上是%2B. -(NSString*)UrlValueEnco ...
- ZH奶酪:哈工大LTP云平台标记含义及性能
从官网搬过来的 囧rz 哈工大讯飞语言云 由哈工大 和科大讯飞 联合研发的中文自然语言处理云服务平台.结合了哈工大“语言技术平台——LTP” 高效.精准的自然语言处理核心技术和讯飞公司在全国性大规模云 ...
- 关于Chrome浏览器(Chrome Stable、 Chrome Canary 、Chromium)
作为开发者,web浏览器一般最常用的可能是Chrome浏览器.但其实Chrome浏览器还有别的一些版本.如:Chrome Stable. Chrome Canary .Chromium.大部分人一般用 ...