Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.

You may assume k is always valid, 1 ≤ k ≤ BST's total elements.

样例

Given root = {1,2}, k = 2, return 2.

/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/ class Solution {
public:
/**
* @param root: the given BST
* @param k: the given k
* @return: the kth smallest element in BST
*/ //迭代遍历到最后(思考如何提前终止)
int count = 0;
int res;
int kthSmallest(TreeNode * root, int k) {
// write your code here
if (root->left != NULL) kthSmallest(root->left, k);
if (++count == k) res = root->val; //①
if (root->right != NULL) kthSmallest(root->right, k);
return res;
} //循环
int kthSmallest(TreeNode * root, int k) {
// write your code here
std::stack<TreeNode*> sta;
while (true) {
while (root) {
sta.push(root);
root = root->left;
}
if (sta.empty()) break;
root = sta.top();
sta.pop();
if(--k==0) return root->val;
root = root->right;
}
}
};

902. Kth Smallest Element in a BST的更多相关文章

  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 找出二叉搜索树中的第k小的元素

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

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

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

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

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

  6. Leetcode 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. Kth Smallest Element in a BST

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

  9. 230. Kth Smallest Element in a BST ——迭代本质:a=xx1 while some_condition: a=xx2

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

随机推荐

  1. C - 继续畅通工程 最小生成树

    省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).现得到城镇道路统计表,表中列出了任意两城镇间修建道路的费用,以及该道路是否已经 ...

  2. .net 解决伪静态下,html页面无法访问

    1.在<system.web>节点下添加: <!--URL重写文件设置开始--> <httpHandlers> <add verb="*" ...

  3. 配置数据库方言——hibernate

    RDBMS 方言 DB2 org.hibernate.dialect.DB2Dialect DB2 AS/400 org.hibernate.dialect.DB2400Dialect DB2 OS3 ...

  4. UVA215-Spreadsheet Calculator(模拟+拓扑排序)

    Problem UVA215-Spreadsheet Calculator Accept:401  Submit:2013 Time Limit: 3000 mSec Problem Descript ...

  5. Linux:Day6(上) egrep、条件测试

    egrep及扩展的正则表达式: egrep = grep -E 扩展正则表达式的元字符: 或者:a | b 练习: 1.显示当前系统root.centos或user1用户的默认shell和UID: 2 ...

  6. shut immediate 数据库遭遇 ORA-24324 ORA-24323

    SQL> shut immediateORA-24324: service handle not initializedORA-24323: value not allowedORA-27140 ...

  7. selenium:断言

    在编写自动化测试脚本时,为了使“机器”去自动辨识test case的执行结果是True还是False,一般都需要在用例执行过程中获取一些信息,来判断用例的执行时成功还是失败. 判断成功失败与否,就涉及 ...

  8. Python学习总结 13 Scrapy

    当前环境是 Win8 64位的,使用的Python 3.5 版本. 一 安装Scrapy 1,安装 lxml pip install lxml -i https://pypi.douban.com/s ...

  9. LOJ6036 编码 2-SAT、Trie

    传送门 每个串只有一个?,?还只能填0或者1,不难想到2-SAT求解. 一个很暴力的想法是枚举?填0或者1,然后对所有可能的前缀连边.这样边数是\(O(n^2)\)的,需要优化. 看到前缀不难想到Tr ...

  10. Vue-插槽学习

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