[抄题]:

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. Windows下python 安装Mysqldb模块

    CMD执行 pip install mysql-python 报错如下: 1.如果报类似 Microsoft Visual C++ 9.0 is required < Unable to fin ...

  2. [UE4]通过代码改变材质

    OrangeMaterial = ConstructorStatics.OrangeMaterial.Get(); , OrangeMaterial); 使用到的结构体如下: struct FCons ...

  3. Oracle跨库复制表结构

    1.首先建立远程连接 create public database link LINK_SJPSconnect to system identified by manager using '(DESC ...

  4. javascript创建对象之原型模式(三)

    少废话,先上代码: function Human() { } Human.prototype.name = "成吉思汗"; Human.prototype.sex = " ...

  5. 详解 Facebook 田渊栋 NIPS2017 论文:深度强化学习研究的 ELF 平台

    这周,机器学习顶级会议 NIPS 2017 的论文评审结果已经通知到各位论文作者了,许多作者都马上发 Facebook/Twitter/Blog/ 朋友圈分享了论文被收录的喜讯.大家的熟人 Faceb ...

  6. Java集合入门

    内容: 1.认识集合 2.Iterator迭代器 1.认识集合 (1)什么是集合 前面的学习,我们知道数据多了,使用数组存放.而且数组中存放的都是基本类型的数据,并且数组是定长的. 当在程序中创建的对 ...

  7. 最近学习下,nohup和&的区别

    nohup是永久执行 &是指在后台运行 运行 nohup --helpRun COMMAND, ignoring hangup signals. 可以看到是“运行命令,忽略挂起信号” 就是指, ...

  8. 3. HashMap和JSONObject用法

    <%@page import="net.sf.json.JSONObject"%><%@page import="java.util.List" ...

  9. 37. CentOS-6.3安装配置Weblogic-10

    安装说明 安装环境:CentOS-6.3-x64软件:server1001_ccjk_linux32.bin安装方式:bin文件安装 安装位置:/usr/local/weblogic/下载地址:htt ...

  10. 定时器setInterval, innerText获取文本, charAt()获取单个字符串, substring(1, content.length)获取范围内的字符串, 实现字符串的滚动效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...