[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

第1小的元素index为0,以此类推,第k小的元素index为k-1

[思维问题]:

知道是写in-order,但是不太记得要加if条件了

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

就是写个in-order,然后get第 k-1个就行了

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

traversal就是要加if

public void inOrderTraversal(List<Integer> result, TreeNode root) {
//Traversal
if (root.left != null) inOrderTraversal(result, root.left);
result.add(root.val);
if (root.right != null) inOrderTraversal(result, root.right);
}

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

Follow up:
What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?

可以变化节点:添加一个traverse的build

第k大不停变化:每个点添加一个总数count标记,然后

if (rootWithCount.left.count == k-1) return rootWithCount.val;

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int kthSmallest(TreeNode root, int k) {
//initialization
List<Integer> result = new ArrayList<Integer>(); //Traversal
inOrderTraversal(result, root); //return
return result.get(k - 1);
} public void inOrderTraversal(List<Integer> result, TreeNode root) {
//Traversal
if (root.left != null) inOrderTraversal(result, root.left);
result.add(root.val);
if (root.right != null) inOrderTraversal(result, root.right);
}
}

230. Kth Smallest Element in a BST 找到bst中的第k小的元素的更多相关文章

  1. [LeetCode] Kth Smallest Number in Multiplication Table 乘法表中的第K小的数字

    Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number ...

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

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

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

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

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

  9. LeetCode OJ 230. Kth Smallest Element in a BST

    Total Accepted: 46445 Total Submissions: 122594 Difficulty: Medium Given a binary search tree, write ...

随机推荐

  1. idea如何禁用SVN

    打开Intellij的setting(ctrl+alt+s),选择plugins,在右边搜索框输入“SVN”,搜索.选择“SVN disconnect”,安装此插件.  插件使用 点击菜单栏中的VCS ...

  2. javascript继承之借用构造函数(二)

    //简单的函数调用 function Father() { this.nums= [1,2]; } function Son() { Father.call(this);//调用超类型,完成son继承 ...

  3. kudu和kudu-impala的安装流程

    安装apache的kudu 第一步:下载rpm包 http://archive.cloudera.com/kudu/redhat/6/x86_64/kudu/5.11.0/RPMS/x86_64/ 第 ...

  4. 常用模块:re ,shelve与xml模块

    一 shelve模块: shelve模块比pickle模块简单,只有一个open函数,所以使用完之后要使用f.close关闭文件.返回类似字典的对象,可读可写;key必须为字符串,而值可以是pytho ...

  5. win7计算机右键属性打不开窗口的解决方法

    原文:http://www.jb51.net/os/windows/169200.html win7计算机右键属性打不开窗口的解决方法 在鼠标右击win7桌面计算机选择属性的时候却发现打不开属性窗口, ...

  6. sqoop2的使用测试

    查看现有link sqoop:000> show link+-----------+------------------------+---------+|   Name    |     Co ...

  7. mysql 修改用户密码

    修改mysql用户密码   目录 mysqladmin命令 UPDATE user 语句 SET PASSWORD 语句 root密码丢失的情况(待验证) mysqladmin命令(回目录) 格式如下 ...

  8. foreach 使用&引用赋值要注意的问题

    <?php $arr = array('a', 'b', 'c'); $arr2 = array('d', 'e', 'f'); foreach($arr as &$value){ $v ...

  9. MySQL数据类型的长度

    MySQL有几种数据类型可以限制类型的"长度",有CHAR(Length).VARCHAR(Length).TINYINT(Length). SMALLINT(Length).ME ...

  10. 机器学习入门-Knn算法

    knn算法不需要进行训练, 耗时,适用于多标签分类情况 1. 将输入的单个测试数据与每一个训练数据依据特征做一个欧式距离. 2. 将求得的欧式距离进行降序排序,取前n_个 3. 计算这前n_个的y值的 ...