[抄题]:

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. js实现手机号身份证等加星(*)号

    下面来为各位整理了一些关于js实现手机号身份证等加星(*)号代码了,在js不足时我们还补充了php实现手机号身份证等加星(*)号的函数,有兴趣的一起来看看.   有时候为了不让用户的手机号码和身份证号 ...

  2. Web API 源码剖析之默认配置(HttpConfiguration)

    Web API 源码剖析之默认配置(HttpConfiguration) 我们在上一节讲述了全局配置和初始化.本节我们将就全局配置的Configuration只读属性进行展开,她是一个类型为HttpC ...

  3. solr4.x之原子更新

    solr4.x发布以后,最值得人关注的一个功能,就是原子更新功能,传说的solr是否能真正的做到像数据库一样,支持单列更新呢? 在solr官方的介绍中,原子更新是filed级别的更新,不会涉及整个Do ...

  4. CSS3基础

    内容: 1.圆角 border-radius 2.阴影 text-shadow.box-shadow 3.渐变 linear.radial 4.rgba rgb+alpha opacity 5.tra ...

  5. 数据库入门4 结构化查询语言SQL

    知识内容: 1.了解SQL 2.库.表操作及索引 3.select语句及insert语句 4.update语句与delete语句 5.SQL常用函数 6.多表连接及组合查询 7.视图操作及数据控制 参 ...

  6. tensorboard启动图

    import tensorflow as tf # 定义一个简单的计算图,实现向量加法的操作. input1 = tf.constant([1.0, 2.0, 3.0], name = 'input1 ...

  7. OpenACC 优化矩阵乘法

    ▶ 按书上的步骤使用不同的导语优化矩阵乘法 ● 所有的代码 #include <iostream> #include <cstdlib> #include <chrono ...

  8. Xshell使用

    xshell更新之后窗口标题没了,多个窗口之后没法切换. 使用ctrl+shift+t恢复 ¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥

  9. Jsch - java SFTP 文件上传下载

    使用Jsch上传.下载文件,核心步骤是:获取channel,然后使用get/put方法下载.上传文件 核心代码句: session = jSch.getSession(ftpUserName, ftp ...

  10. DB2 to mysql

    1.安装DB2 create database testdb connect to testdb 2. 将DB2 ixf格式into DB2 IMPORT FROM /tabxxx.IXF OF IX ...