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. 【Linux基础】VM使用

    VM三种联网方法和原理 (1)Bridged桥接 使用VMnet0虚拟交换机,此时虚拟机相当与网络上的一台独立计算机与主机一样,拥有一个独立的IP地址,所有机器均可互访,可以联网.使用桥接方式,A,A ...

  2. nginx基本配置与参数说明

    user nobody; #启动进程,通常设置成和cpu的数量相等 worker_processes  1;   #全局错误日志及PID文件 #error_log  logs/error.log; # ...

  3. vue中$router.push打开新窗口

    在vue中使用 this.$router.push({ path:  '/home' }) 默认是替代本窗口 如果想新开一个窗口,可以使用下面的方式: let routeData = this.$ro ...

  4. java 关于打断点

    比如:前台传过来参数中文乱码,需要decode才可以使用, 判断问题. debug 在 DispatcherServlet OncePerRequestFilter 打断点, 查看前台过来的中文在哪里 ...

  5. 洛谷P1127-词链

    Problem 洛谷P1127-词链 Accept: 256    Submit: 1.3kTime Limit: 1000 mSec    Memory Limit : 128MB Problem ...

  6. 配置DispatcherServlet应该写/还是/*

    相亲怎么做 web应用需要放在Tomcat容器中才能启动,Tomcat容器内有一个默认的web.xml文件,在自己项目中配置的XML文件都是继承自Tomcat中的全局XML文件并重写其中相应配置,这种 ...

  7. go标准库的学习-os

    参考https://studygolang.com/pkgdoc 导入方式: import "os" os包提供了操作系统函数的不依赖平台的接口 一开始简单举个例子: packag ...

  8. Emacs 自动补全插件 ycmd

    Emacs 自动补全,最好的插件当属 ycmd.以下记录我的安装过程. 1. 安装 ycmd server github 官方地址: https://github.com/Valloric/ycmd ...

  9. <unix网络编程>UDP套接字编程

    典型的UDP客户/服务器程序的函数调用如下: 1.缓冲区 发送缓冲区用虚线表示,任何UDP套接字都有发送缓冲区,不过该缓冲区仅能表示写到该套接字的UDP数据报的上限.如果应用进程写一个大于套接字缓冲区 ...

  10. Angularjs 过滤器使用

    Filter:格式化数据 // HTML表达式: {{ filter_expression | filter : expression : comparator}}   // JS表达式: $filt ...