[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- ...
随机推荐
- React组件设计(转)
React组件设计 组件分类 展示组件和容器组件 展示组件 容器组件 关注事物的展示 关注事物如何工作 可能包含展示和容器组件,并且一般会有DOM标签和css样式 可能包含展示和容器组件,并且不会有D ...
- 转载>>ASCII、UTF8、Uncicode编码下的中英文字符大小
原地址:http://www.tracefact.net/CSharp-Programming/Network-Programming-Part2.aspx ASCII.UTF8.Uncicode编码 ...
- LeetCode 39 Combination Sum(满足求和等于target的所有组合)
题目链接: https://leetcode.com/problems/combination-sum/?tab=Description Problem: 给定数组并且给定一个target,求出所 ...
- linux中的标准输出和输入
===============1.有些人经常问我这个问题问题=========== 经常在脚本里面看到这个 2>&1 表示什么意思啊? ==============2.理论 ...
- ldap 配置过程详解
ldap常用名称解释 1.环境搭建 操作系统:centos6.5 x86_64关闭防火墙.selinux开启时间同步# crontab -e加入# time sync*/5 * * * * /usr/ ...
- php应用
1. php判断是否为数字 is_numeric() 这个函数就是检测参数是否为数字,如果是就返回true,如果不是就返回false is_numeric( 'abcd123' ) or die('提 ...
- Edge Animate使用SPRITESHEET创建动画(三)
在Flash动画制作中,使用SpriteSheet制作动画元素是一个常见和普遍的方法.在Edge Animate中,我们也可以利用SpriteSheet来制作HTML5动画.本文将从一个示例出发,介绍 ...
- 《C与指针》——高级指针话题
指针真是让人又爱又恨..... 首先还是先来看一下C语言中的高级指针声明.不要被表面迷惑最重要. /* ** <C和指针>——高级指针话题 */ int i; //定义一个整型变量 int ...
- 判断强联通图中每条边是否只在一个环上(hdu3594)
hdu3594 Cactus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- 转Python SciPy库——拟合与插值
1.最小二乘拟合 实例1 import numpy as np import matplotlib.pyplot as plt from scipy.optimize import leastsq p ...