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 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小的元素的更多相关文章
- [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 ...
- [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素
题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- (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 ...
- [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 ...
- LeetCode OJ 230. Kth Smallest Element in a BST
Total Accepted: 46445 Total Submissions: 122594 Difficulty: Medium Given a binary search tree, write ...
随机推荐
- Ubuntu12.10下Vsftpd的安装
安装Vsftpd sudo apt-get install vsftpd 配置 sudo vim /etc/vsftpd.conf 取消以下两行前面的注释 local_enable=YES write ...
- API网关Kong系列(三)添加服务
进入之前部署好的kong-ui,默认第一次登陆需要配置kong服务的地址 进入API菜单,点击+号 按照要求填入相关信息 至此完成,可以使用诸如 https://your.domain.com:208 ...
- securecrt8注册码
securecrt8注册码,两个可用 Name:meisiCompany:TEAM ZWTSerial Number:03-14-367662License Key:ACCFAX R9FHJ7 QZV ...
- Linux操作系统,服务器端的主流
1.无意之间,一直使用的Windows,其实也是在Unix的基础之上开发的,什么xp,win7等都是,就是Unix的一些堆砌,其实这样说也不对.但是windows的复杂程度比Linux要复杂,因为Wi ...
- 教Alexa看懂手语,不说话也能控制语音助手
Alexa.Siri.小度……各种语音助手令人眼花缭乱,但这些设备多是针对能力健全的用户,忽略了听.说能力存在障碍的人群.本文作者敏锐地发现了这一 bug,并训练亚马逊语音助手 Alex 学会识别美式 ...
- 小朋友学C++(2)
多态 (一) 先编写函数: #include <iostream> using namespace std; class Shape { protected: int width, hei ...
- tornado-简单的服务器
安装tornado pip install tornado 安装sqlalchemy 1.大概代码的解释 import tornado.ioloop #开启循环,等待访问 import tornado ...
- selenium自动化测试通过localstorage绕过登陆
引言: 做自动化测试,尤其是通过page object模式做UI自动化测试,登陆是个很麻烦的事情,比如你想对某个页面进行测试,一般直接链接到那个页面是不可能的,总是需要先登陆,然后刷新页面才能到想要的 ...
- leetcode138
/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...
- leetcode530
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...