[LeetCode] 230. Kth Smallest Element in a BST_Medium tag: Inorder Traversal
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
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?
这个题目如果不考虑follow up, 就很简单了, 用LeetCode questions conlusion_InOrder, PreOrder, PostOrder traversal里面inOrder traversal的方式, 然后返回ans[k-1]即可.
T: O(n), S: O(n) 因为stack的Space已经是O(n), 所以是否省掉ans的space意义不大.
至于follow up如果经常被modified的话, 我们可以将LIstNode的structure加入一个parent指向他的parent, 然后有一个kth element指针, 当delete的值大于指针的值, 不变, 如果小于的话,就将kth element向左移, 如果insert的值大于指针的值, 不变, 同理如果小于指针的值, 向左移动.
code
1. recursive
class Solution:
def kthSmall(self, root, k):
def helper(root):
if not root: return
helper(root.left)
ans.append(root.val)
helper(root.right)
ans = []
helper(root)
return ans[k-1]
2. iterable
class Solution:
def kthSmall(self, root, k):
stack = []
while stack or root:
if root:
stack.append(root)
root = root.left
else:
node = stack.pop()
k-= 1
if k == 0: return node.val
root = node.right
[LeetCode] 230. Kth Smallest Element in a BST_Medium tag: Inorder Traversal的更多相关文章
- [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 ...
- (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 ...
- Java for LeetCode 230 Kth Smallest Element in a BST
解题思路: 直接修改中序遍历函数即可,JAVA实现如下: int res = 0; int k = 0; public int kthSmallest(TreeNode root, int k) { ...
- LeetCode 230 Kth Smallest Element in a BST 二叉搜索树中的第K个元素
1.非递归解法 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...
- LeetCode 230. Kth Smallest Element in a BST 动态演示
返回排序二叉树第K小的数 还是用先序遍历,记录index和K进行比较 class Solution { public: void helper(TreeNode* node, int& idx ...
- 【LeetCode】230. Kth Smallest Element in a BST
Difficulty: Medium More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/kth-smallest- ...
随机推荐
- DRM in Android详解--转
DRM,英文全称为Digital Rights Management,译为数字版权管理.它是目前业界使用非常广泛的一种数字内容版权保护技术.随着知识产权保护受重视的程度日益提高,快速攻城略地得Andr ...
- Makefile eval函数
https://www.cnblogs.com/gaojian/archive/2012/10/04/2711494.html对 makefile 中 eval 函数的学习体会 http://blog ...
- LeetCode 10 Regular Expression Matching(字符串匹配)
题目链接 https://leetcode.com/problems/regular-expression-matching/?tab=Description '.' Matches any si ...
- 笨鸟就要勤奋&专注
最近两天在找工作的过程中颇受打击,两家高大上的公司看起来就是要收集世界上最聪明的人~,在参加G家的online test之前还天真的认为一不小心通过了怎么办呢?考完试之后才发现真的是想多了,关于题目看 ...
- Spark2 Dataset分析函数--排名函数row_number,rank,dense_rank,percent_rank
select gender, age, row_number() over(partition by gender order by age) as rowNumber, ...
- &与&&, |与||区别
&和|称为短逻辑符,&&及||称为长逻辑符.长逻辑符只比较左边和右边的第一个元素,而短逻辑符会比较所有的 > a<-c(TRUE, FALSE, TRUE, FAL ...
- tkinter 提示符
在python3.4中,原来的tkMessageBox变成tkinter.messagebox,使用方式如下: import tkinter.messagebox tkinter.messagebox ...
- 0001python中特殊的for迭代zip函数
>>> a = [1,2,3,4,5] >>> b = [9,8,7,6,5] >>> length = len(a) if len(a)< ...
- MySQL ·InnoDB 文件系统之文件物理结构
从上层的角度来看,InnoDB层的文件,除了redo日志外,基本上具有相当统一的结构,都是固定block大小,普遍使用的btree结构来管理数据.只是针对不同的block的应用场景会分配不同的页类型. ...
- iOS安装包重签笔记
https://blog.csdn.net/skylin19840101/article/details/60583893