题目:

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. java——二叉树面试题

    import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; import java.util ...

  2. java集合框架——List

    一.List接口概述 List有个很大的特点就是可以操作角标. 下面开始介绍List接口中相对于Collection接口比较特别的方法.在Collection接口中已经介绍的方法此处就不再赘述. 1. ...

  3. CSS:响应式下的折叠菜单(条纹式)

    原文:CSS: Responsive Navigation Menu 译文:CSS:响应式导航菜单 译者:dwqs 写在之前,关于如何制作响应式的下拉菜单:响应式下的下拉菜单 之前,我写了一篇关于怎么 ...

  4. jQuery实现网页右下角悬浮层提示

    最近有同事提到类似网页右下角的消息悬浮提示框的制作.我之前也做过一个类似的例子,很简单.是仿QQ消息.现在感觉之前的那个例子只是说了实现原理,整体上给你的感觉还是太丑,今天为大家带来一个新的例子.是D ...

  5. Netbackup-Oracle数据库恢复演练(上)

    目录 第一章NBU实施内容 1 第二章 环境准备 1 2.1添加LICENSE: 1 2.2安装配置NBU客户端(linux,unix相同) 2 2.3配置并验证oracle备份脚本 3 2.4添加备 ...

  6. Java从入门到放弃——02.常量、变量、数据类型、运算符

    本文目标 理解什么是常量,什么是变量 认识八大基本数据类型 了解算数运算符.赋值运算符.关系运算符.逻辑运算符.位运算符.三元运算符 1.什么是常量与变量? 常量是相对静止的量,比如整数:1,2,3 ...

  7. Getting aCC Error :name followed by "::" must be a class or namespace name"

    Getting aCC Error :name followed by "::" must be a class or namespace name" 原始是这样子的: ...

  8. centos安装django

    1.如果默认安装的是python2.6,先升级至python2.7 参考:http://www.cnblogs.com/tiger2soft/p/5677843.html 2.安装pip 先下载get ...

  9. powerdesigner15 反向工程

  10. input的placeholder的颜色、字号、边距设置

    #scbar_txt::-webkit-input-placeholder { /* WebKit browsers */    text-indent: 10px; font-size:16px; ...