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. bzoj 1101 Zap —— 莫比乌斯反演

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1101 直接莫比乌斯反演. 代码如下: #include<cstdio> #inc ...

  2. 《Kubernetes权威指南第2版》学习(四)kubernetes基本概念和术语

    1: etcd是干什么的: 键-值存储仓库,用来配置共享和服务发现. k8s把Node, pod,replication controller, Services看做是资源对象,这些资源对象可以通过K ...

  3. CentOS 7 配置 ISCSI 服务器

    一.服务器配置 1.安装配置软件: yum install targetcli -y 2.进入配置界面: targetcli ,并进行如下配置: 1) /backstores/block create ...

  4. Express的日志模块morgan

    morgan 是nodejs的一个日志模块,由 express 团队维护. 这里通过示例简要介绍morgan模块在express中的应用,大部分示例直接来自于.morgan的文档:https://gi ...

  5. assert.equal()

    assert.equal(actual, expected[, message]) 使用相等运算符(==)测试 actual 参数与 expected 参数是否相等(通俗解释equal方法接受三个参数 ...

  6. C++中的友元小结

    我们知道,在一个类总可以有公有的(public)成员和私有的(private)成员.在类外可以访问公用成员,只有本类中的函数可以访问本类的私有成员. 现在,我们学习一种新的情况--友元. 在C++中, ...

  7. pig 入门教程(1)

    出处:http://www.codelast.com/ 本文可以让刚接触pig的人对一些基础概念有个初步的了解. 本文大概是互联网上第一篇公开发表的且涵盖大量实际例子的Apache Pig中文教程(由 ...

  8. linux 统计 程序 运行时间

    测试 代码运行时间 linux 中的 <sys/time.h> 中 有个函数可以获取当前时间,精确到 微秒 ---->  gettimeofday() #include <sy ...

  9. 前端项目中gulp的使用

    在公司项目开发中,有一个前端项目,我们使用gulp来生成目标文件(css,js,html文件) 进入到这个项目目录中  C:\My Project\FrontEnd\TestBuilder 然后依次运 ...

  10. JavaScript学习系列1 基础-变量

    1. 在一次面试中,被问到JavaScript中使用严格模式有什么区别, 这里的严格模式指的是 ECMAScript5严格模式 我们首先来看看普通模式,也就是非严格模式 a = 5; console. ...