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.

直观地一想。查找第k小的数,不就是遍历到第k个数吗?所以中序遍历非常easy想到,例如以下:

/**
* 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*> nodeStack;
TreeNode* tmp=root;
int kthMin = 0;
int kthValue;
while ((!nodeStack.empty() || tmp!=NULL) && kthMin!=k)
{
while (tmp != NULL)
{
nodeStack.push(tmp);
tmp = tmp->left;
}
tmp = nodeStack.top();
nodeStack.pop();
kthMin++;//对中序遍历稍加改动
kthValue = tmp->val;
tmp = tmp->right;
}
return kthValue;
}
};

另外。看了一下网友的解答,很巧妙。

他是先统计左子树上节点个数,假设节点个数小于k。则在右子树上找第k-n-1小的数,假设刚为k则就是当前节点,假设大于k,则继续在左子树上找第k小的数。

只是。每次递归都要统计一次节点个数,会不会导致复杂度添加?

int kthSmallest(TreeNode* root, int k) {
if (!root) return 0;
if (k==0) return root->val; int n=count_size(root->left);
if (k==n+1) return root->val; if (n>=k){
return kthSmallest(root->left, k);
}
if (n<k){
return kthSmallest(root->right, k-n-1);
} } int count_size(TreeNode* root){
if (!root) return 0;
return 1+count_size(root->left)+count_size(root->right); }

leetCode(46):Kth Smallest Element in a BST的更多相关文章

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

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

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

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

  4. (medium)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 ...

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

  6. Java for LeetCode 230 Kth Smallest Element in a BST

    解题思路: 直接修改中序遍历函数即可,JAVA实现如下: int res = 0; int k = 0; public int kthSmallest(TreeNode root, int k) { ...

  7. LeetCode 230 Kth Smallest Element in a BST 二叉搜索树中的第K个元素

    1.非递归解法 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...

  8. LeetCode 230. Kth Smallest Element in a BST 动态演示

    返回排序二叉树第K小的数 还是用先序遍历,记录index和K进行比较 class Solution { public: void helper(TreeNode* node, int& idx ...

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

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

随机推荐

  1. 学习ASP.NET MVC系列 - 还有比这更简炼的吗?把复杂的事情变简单了,贡献啊!

    转自

  2. 2016.02.25,英语,《Vocabulary Builder》Unit 02

    ag:来自拉丁语do.go.lead.drive,an agenda是要做事情的清单,an agent是代表他们做事的人,同时也是为他人做事的机构.拉丁语litigare包括词根lit,即lawsui ...

  3. HDU 5672 String 尺取法追赶法

    String Problem Description There is a string S.S only contain lower case English character.(10≤lengt ...

  4. Linux Mint (应用软件— 虚拟机:Virtualbox续)

    我已经在当前的电脑中安装好了虚拟机.并且在虚拟机中安装了Ubuntu14.04LTS系统.接下来能够開始自己的折腾之旅了. 開始使用的时候总是感觉显示有问题,根据经验来看同,是系统分辨率设置不当引起的 ...

  5. 0x15 KMP

    这个算法本身就不难. poj1961 #include<cstdio> #include<iostream> #include<cstring> #include& ...

  6. swift-初探webView与JS交互

    公司接下来的项目需要用swift内嵌h5来实现, 以前没有做过swift项目, 现在慢慢将所学的一点一滴记录一下 一个是怕自己忘了- =, 再就是希望大家看到能帮助我哈哈哈 前几天想要直接用swift ...

  7. xargs用例一个

    ls -a *.doc|awk -F. '{print $1}' |xargs -I {} java -jar ~/soft/jodconverter-2.2.2/lib/jodconverter-c ...

  8. CXF2.7.7 java.lang.RuntimeException: Cannot create a secure XMLInputFactory

    cxf-2.7.7.jar neethi-3.0.2.jar stax2-api-3.1.1.jar woodstox-core-asl-4.2.0.jar wsdl4j-1.6.3.jar xmls ...

  9. 50个极好的bootstrap 后台框架主题下载

    50个极好的bootstrap 后台框架主题下载 http://sudasuta.com/bootstrap-admin-templates.html 越来越多的设计师和前端工程师开始用bootstr ...

  10. jQuery进度条设置

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta http-equiv="con ...