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. ubuntu14 编译tensorflow C++ 接口

    tensorflow1.11 bazel 0.15.2 protobuf 3.6.0 eigen 3.3.5 wget -t 0 -c https://github.com/eigenteam/eig ...

  2. 量化投资_关于Multicharts砖型图(传统砖型图和非传统砖型图)最详细的解释_调用篇

    1. 砖图形成数组后,我们再对他们复制到数组中然后再调用出来看一下. 2. 如下是累计只取20个数组大小为例,如果开始阶段数组长度组成初始的Array[20]的长度,然后这个数组的最后一个值填满后,进 ...

  3. LAMP环境搭建,防火墙开启,数据库挂载在逻辑卷

    具体要求: 1. 源码部署 LAMP 环境, 和生产保持一致 2. 静态资源文件同步生产环境 3. 需要同时部署 2 个 web 网站 步骤: 一. 需要的安装包. 提前准备好. apr-util-1 ...

  4. 第一个----关于GPIO的总结

    首先,自己本来报的是单片机的  ,但是因为队友的脑残,给我报成了嵌入式,哎,惨啊,就得从头看这个云里雾里的东西,但是没办法,都报名了  不能呢个交白卷,不然自己就是逃兵了,还有20天就比赛了  我得加 ...

  5. set theory

    set theory Apart from classical logic, we assume the usual informal concept of sets. The reader (onl ...

  6. 写入简单的日志log

    log.c: #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <string.h& ...

  7. 吴裕雄--天生自然C语言开发:强制类型转换

    #include <stdio.h> int main() { , count = ; double mean; mean = (double) sum / count; printf(& ...

  8. C - The Battle of Chibi HDU - 5542 (树状数组+离散化)

    Cao Cao made up a big army and was going to invade the whole South China. Yu Zhou was worried about ...

  9. hdu2876 Connections between cities(LCA倍增)

    图不一定联通,所以用并查集找各个联通块的祖先分别建图,之后就和LCA的步骤差不多了 #include<iostream> #include<cstring> #include& ...

  10. day04-函数,装饰器初成

    面试的时候,经常被问过装饰器,所以掌握好装饰器非常重要. 一.装饰器形成的过程:1.最简单的装饰器.2.被装饰的函数有返回值.3.被装饰的函数有一个参数.4.被装饰的函数有多个位置参数.5.被装饰的函 ...