1、非递归解法

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

2、递归解法

 /**
* 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) {
return helper(root, k);
}
int helper(TreeNode* root, int &k) {
if (!root)
return -;
int val = helper(root->left, k);
if (k == )
return val;
if (--k == )
return root->val;
return helper(root->right, k);
}
};

LeetCode 230 Kth Smallest Element in a BST 二叉搜索树中的第K个元素的更多相关文章

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

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

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

    给定一个二叉搜索树,编写一个函数kthSmallest来查找其中第k个最小的元素. 注意:你可以假设k总是有效的,1≤ k ≤二叉搜索树元素个数. 进阶:如果经常修改二叉搜索树(插入/删除操作)并且你 ...

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

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

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

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

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

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

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

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

随机推荐

  1. TVYJ1266:费解的开关

    我对状态空间的理解:https://www.cnblogs.com/AKMer/p/9622590.html 题目传送门:http://www.joyoi.cn/problem/tyvj-1266 这 ...

  2. JS---星星评分

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  3. ES6学习之正则扩展

    RegExp正则函数 var reg = new RegExp("abc","igm"); //等价于 var reg = new RegExp(/abc/ig ...

  4. kvm基础 虚拟机内存、CPU调整

    转自http://blog.csdn.net/hnhuangyiyang/article/details/50902223 一.调小虚拟机内存 调小虚拟机内存可以动态实现,不用关机1.查看当前内存大小 ...

  5. k8s基础(3)etcd集群

    下载安装 https://github.com/coreos/etcd/releases 在这网页,可以看到有多个版本共选择. 下载3.25 解压后, cd etcd-v3.2.5-linux-amd ...

  6. restful用法

    http://www.cnblogs.com/wen-wen/p/6149847.html一.创建services文件夹services文件夹下1.BaseService.jsclass Servic ...

  7. Ubuntu Linux 使用桂电校园网 上网

    2016年9月1日 星期四 桂电校园网今天升级新的出校器,旧的出校器已经不能使用,所以本篇博客已经过期,下面的方法已经不能让Ubuntu使用桂电校园网上外网了.详细的原因,请到这个网站查看:校园网计费 ...

  8. Hibernate存储date/datetime问题,解决java的date与mysql的datetime不兼容

    主要原因是出在Hibernate的配置文件中. 症状 1. java文件类型java.util.Date,数据库类型datetime,Hibernate配置文件用date,存储进数据库的时间只有年月日 ...

  9. JAVA IO包的整理---------Exception

    EOFException Signals that an end of file or end of stream has been reached unexpectedly during input ...

  10. 阿里云 Ubuntu 服务器 安装 pymssql

    我是按照官方的说明文档: http://www.pymssql.org/en/stable/intro.html#getting-started 一步一步操作的,但是没有成功,在网上搜索了很多信息,最 ...