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. Split string every nth character?

    https://stackoverflow.com/questions/9475241/split-string-every-nth-character >>> line = '12 ...

  2. Jisa's Notebook

    同步IO和异步IO,阻塞IO和非阻塞IO分别是什么,到底有什么区别?不同的人在不同的上下文下给出的答案是不同的.所以先限定一下本文的上下文. 本文讨论的背景是Linux环境下的network IO. ...

  3. 寒假day04

    今天编写了毕设系统中的专家画像模块,实现了人员标签的建立与划分,同时刷了牛客网的面试相关题目. 1.如果系统的umask设置为244,创建一个新文件后,它的权限:(C) --w-r--r-- -r-x ...

  4. 题解 P5837 【[USACO19DEC]Milk Pumping】

    这题其实想法挺简单的,因为他只需要简单的把每个点的花费和流量用dp记下来就好了 1.怎么记: 首先考虑dp的状态.由于所在的点和流量都要记,所以dp开二维,一维记所在的点,另一维记去哪 //dp[i] ...

  5. POJ 3080 Blue Jeans (求最长公共字符串)

    POJ 3080 Blue Jeans (求最长公共字符串) Description The Genographic Project is a research partnership between ...

  6. Pytorch的19种损失函数

    基本用法 12 criterion = LossCriterion() loss = criterion(x, y) # 调用标准时也有参数 损失函数 L1范数损失:L1Loss 计算 output ...

  7. 吴裕雄--天生自然python Google深度学习框架:图像识别与卷积神经网络

  8. Android开发学习3

    学习内容: 1.复选框CheckBox 2.ImageView & 使用第三方库加载网络图片 3.列表视图ListView 4.网格视图GridView 5.ScrollView & ...

  9. Android之UI View与ViewGroup

    1.基本概念 View:所有可视化控件的父类,Android App屏幕上用户可以交互的对象(例如 按钮 下拉框 文本框等). ViewGroup:View的子类,存放View和ViewGroup对象 ...

  10. iTOP-iMX6UL开发板-MiniLinux-CAN测试使用文档

    本文档介绍的是迅为iMX6UL开发板在 MiniLinux 系统环境下 iTOP-iMX6UL CAN 实验调试步骤.给用户提供了“can_libs.rar”.“can_tools.zip”和“can ...