题目:

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?

Hint:

  1. Try to utilize the property of a BST.
  2. What if you could modify the BST node's structure?
  3. The optimal runtime complexity is O(height of BST).

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) {
int count = ;
stack<TreeNode*> sta;
TreeNode* curr = root;
while ( !sta.empty() || curr )
{
if ( curr )
{
sta.push(curr);
curr = curr->left;
}
else
{
curr = sta.top();
sta.pop();
count++;
if ( count==k ) return curr->val;
curr = curr->right;
}
}
return curr->val;
}
};

tips:

直观的做法是中序遍历BST,然后获得第k个元素。

【Kth Smallest Element in a BST 】cpp的更多相关文章

  1. 【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 ...

  2. 【刷题-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 ...

  3. [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素

    题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...

  4. LeetCode 230. 二叉搜索树中第K小的元素(Kth Smallest Element in a BST)

    230. 二叉搜索树中第K小的元素 230. Kth Smallest Element in a BST 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的 ...

  5. 【LeetCode】230. Kth Smallest Element in a BST

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/kth-smallest- ...

  6. 【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 ...

  7. 【树】Kth Smallest Element in a BST(递归)

    题目: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. ...

  8. 【LeetCode】230. 二叉搜索树中第K小的元素 Kth Smallest Element in a BST

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:算法题,刷题,Leetcode, 力扣,二叉搜索树,BST ...

  9. [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 ...

随机推荐

  1. hdu-2609 How many---最小表示法模板+set判重

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2609 题目大意: 有n个有01组成的字符串,每个字符串都代表一个项链,那么该字符串就是一个环状的结构 ...

  2. 判断团队适不适合使用node

    1.要不要用 2.历史包袱 3.跟进升级 看完scott创业公司使用node,对于一个团队要不要使用node,第一个就是如果承接的项目有很多历史迭代,线上也在稳定的抛,不要轻易的替换,比如很多老代码, ...

  3. 2017.9.18 HTMl学习总结----input标签的额type

    2.1.3  HTML表单标签与表单设计 (1)表单的组成:文本框(text),密码框(password),多行文本框(Multiline text box).  单选按钮框(Single - rad ...

  4. SVN知识集合

    1. 如果某个项目之前保存了A仓库的信息,无法切换到B仓库(通过AnkhSVN或者VisualSVN),可以先在本地去除版本控制(用TortoiseSVN),然后导出B仓库信息(用TortoiseSV ...

  5. java导入、导出

    一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际 ...

  6. C语言文件操作类型速查

    文件使用方式 含义 "r"(只读) 为输入打开一个文本文件,不存在则失败 "w"(只写) 为输出打开一个文本文件,不存在则新建,存在则删除后再新建 " ...

  7. v-show

    v-show的原理是当值为false的时候,元素display:none  隐藏了元素且脱离文档流,但是在dom 中仍然存在. 与v-if使用场景不同,文档中提到,当需要高频切换的时候使用v-show ...

  8. BZOJ2752: [HAOI2012]高速公路(road)(线段树 期望)

    Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 1820  Solved: 736[Submit][Status][Discuss] Descripti ...

  9. flask实现基于elasticsearch的关键词搜索建议

    1.实现效果 2.fuzzy模糊查询和suggest查询 fuzzy模糊查询 GET chaxun/job/_search { "query": { "fuzzy&quo ...

  10. Linux实战教学笔记05:远程SSH连接服务与基本排错

    第1章 远程连接LInux系统管理 1.1 为什么要远程连接Linux系统 在实际的工作场景中,虚拟机界面或物理服务器本地的窗口都是很少能够接触到的,因为服务器装完系统后,都要拉到IDC机房托管,如果 ...