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.

Example 1:

Input: root = [3,1,4,null,2], k = 1
3
/ \
1 4
\
  2
Output: 1

Example 2:

Input: root = [5,3,6,2,4,null,null,1], k = 3
5
/ \
3 6
/ \
2 4
/
1
Output: 3
 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int res;
int count;
public int kthSmallest(TreeNode root, int k) {
res = 0;
count = k;
inOrder(root);
return res;
} private void inOrder(TreeNode root) {
if (root == null) {
return;
}
inOrder(root.left);
count -= 1;
if (count == 0) {
res = root.val;
return;
}
inOrder(root.right);
}
}

[LC] 230. 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 (2 solutions)

    Kth Smallest Element in a BST Given a binary search tree, write a function kthSmallest to find the k ...

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

  4. 230. Kth Smallest Element in a BST 找到bst中的第k小的元素

    [抄题]: Given a binary search tree, write a function kthSmallest to find the kth smallest element in i ...

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

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

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

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

随机推荐

  1. springboot和shiro的整合

    直接贴上代码 1. 所需要的jar包 <dependency> <groupId>org.apache.shiro</groupId> <artifactId ...

  2. Elasticsearch节点类型

    当我们启动Elasticsearch的实例,就会启动至少一个节点.相同集群名的多个节点的连接就组成了一个集群. 在默认情况下,集群中的每个节点都可以处理http请求和集群节点间的数据传输,集群中所有的 ...

  3. HTTP、MQTT、WebSocket有什么区别

    https://blog.csdn.net/linyunping/article/details/81950185 相同点:均为OSI 7层模型(应用层.表示层.会话层.传输层.网络层.数据链路层.物 ...

  4. 远程SSH服务使用指南

    Author Email Yaoyao Liu yaoyaoliu@msn.com 本文所有教程以ubuntu为例,对其他unix内核系统如Debian.CentOS.macOS等也适用. 目录 安装 ...

  5. c++语法(2)

    #include<iostream> #include<windows.h> using namespace std; class Parents { public: virt ...

  6. 前端-HTLM

    前端简介: 什么是前端? 任何与用户直接打交道的操作界面都可以被称为前端,如:网页界面,手机界面.... 前端的学习历程和内容: 要学习的内容: 三大重点: 1.Web服务的本质: 浏览器中敲入网址回 ...

  7. hashlib python 加密框架

    python3中digest()和hexdigest()区别 转自:https://www.cnblogs.com/yrxns/p/7727471.html hashlib是涉及安全散列和消息摘要,提 ...

  8. S5P4418开发板android源码下uboot和内核缺省文件的配置

    uboot 需要配置缺省文件,进入解压的源码目录 android,然后进入 u-boot 目录,如下图所示.如上图所示,如果是 1G 核心板,则使用“cp nsih-1G16b-4418.txt ns ...

  9. 吴裕雄--天生自然C语言开发:递归

    void recursion() { statements; ... ... ... recursion(); /* 函数调用自身 */ ... ... ... } int main() { recu ...

  10. CCS|ANSI|中华人民共和国标准化法|国标|ISO|IEC|Ieeexplore|

    国家的标准的有效期,标龄是5年.强制性标准是是指为保障人体的健康.人身.财产安全的标准和法律.行政法规定强制执行的标准,如药品标准.食品卫生标准. CCS:分类法简写图 国际标准,比如美国国家标准AN ...