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?

Show Hint

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)的更多相关文章

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

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

  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】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)

    [LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

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

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

  6. LeetCode OJ 230. Kth Smallest Element in a BST

    Total Accepted: 46445 Total Submissions: 122594 Difficulty: Medium Given a binary search tree, write ...

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

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

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

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

随机推荐

  1. LigerUi之Grid使用详解(二)——数据编辑

    一.问题概述 在开发web信息管理系统时,使用Web前端框架可以帮助我们快速搭建一组风格统一的界面效果,而且能够解决大多数浏览器兼容问题,提升开发效率.所以上一篇文章为大家介绍了LigerGrid的显 ...

  2. Spring(十):Spring配置Bean(三)Bean的作用域、使用外部属性文件

    Bean的作用域: 支持四种配置,分别是singleton,prototype,request,session. singleton 默认情况下在spring confinguration xml文件 ...

  3. 转:查看linux系统版本号

    转自: http://blog.csdn.net/zhuying_linux/article/details/6859286 lsb_release -a

  4. 自己定义View时,用到Paint Canvas的一些温故,简单的帧动画(动画一 ,&quot;掏粪男孩Gif&quot;顺便再提提onWindowFocusChanged)

    转载请注明出处:王亟亟的大牛之路 之前在绘画的过程中提到了静态的旋转啊,缩放啊,平移等一些效果.那么自己定义的View当然也有动态的效果也就是我们的Animation.经常使用的有三种 View An ...

  5. 配置 Windows 下的 nodejs C++ 模块编译环境 安装 node-gyp

    配置 Windows 下的 nodejs C++ 模块编译环境 根据 node-gyp 指示的 Windows 编译环境说明, 简单一句话就是 "Python + VC++ 编译环境&quo ...

  6. Elasticsearch之集群脑裂

    https://www.cnblogs.com/zlslch/p/6477312.html

  7. jQuery 用each后添加click

    mydd = $('.plist'); mydd.each(function(i){ $(this).click(function(){ mydl.eq(i).hide("slow" ...

  8. JS获取当前/指定URL参数

    方法: 首先通过 document.location 获得当前访问网页的网址, 其次用 split 方法通过“?”把网址分为两部分. 如果网址中有参数(arrObj.length > 1) 再用 ...

  9. VMware虛擬化技術實作問答

    http://www.netadmin.com.tw/article_content.aspx?sn=1202130002&ns=1203280001&jump=3 Q4:啟用VMwa ...

  10. vcenter建立cluster

    参见: VMware vSphere 5.1 学习系列之六:vCenter Server 主机管理(2) http://bbs.watchstor.com/thread-166222-1-2.html ...