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的更多相关文章

  1. [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素

    题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...

  2. [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 ...

  3. 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 ...

  4. (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 ...

  5. [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 ...

  6. Java for LeetCode 230 Kth Smallest Element in a BST

    解题思路: 直接修改中序遍历函数即可,JAVA实现如下: int res = 0; int k = 0; public int kthSmallest(TreeNode root, int k) { ...

  7. LeetCode 230 Kth Smallest Element in a BST 二叉搜索树中的第K个元素

    1.非递归解法 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...

  8. LeetCode 230. Kth Smallest Element in a BST 动态演示

    返回排序二叉树第K小的数 还是用先序遍历,记录index和K进行比较 class Solution { public: void helper(TreeNode* node, int& idx ...

  9. 【LeetCode】230. Kth Smallest Element in a BST

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/kth-smallest- ...

随机推荐

  1. win7 查看当前java路径

    C:\Users\zh>where javaC:\Windows\System32\java.exeD:\TOOL\jdk1.8.0_91\bin\java.exeD:\TOOL\jdk1.8. ...

  2. SSH安装篇之——SecureCRT连接(内网和外网)虚拟机中的Linux系统(Ubuntu)

    最近在学习Linux,看了网上很多SecureCRT连接本地虚拟机当中的Linux系统,很多都是需要设置Linux的配置文件,有点繁琐,所以自己就摸索了一下,把相关操作贴出来分享一下. SecureC ...

  3. 5-5 ES6的模块化的基本规则或特点

    一.AMD, CMD, CommonJs和ES6对比 1.AMD ==> 是RequireJS在推广过程中对模块定义的规范化产出 // RequireJS定义标准, 导步加载依赖, 依赖前置 d ...

  4. 2-3 vue配置介绍

    一.通过vue-cli构建的项目的文件介绍 1.bulid文件夹 ==> 项目打包的配置文件夹 2.config文件夹 ==> 打包的配置 3.src文件夹 ==> 项目开发的源码 ...

  5. Jdk提供的动态代理示例

    package com.jiaoyiping.util.demo; import java.lang.reflect.InvocationHandler; import java.lang.refle ...

  6. HTML5 Canvas 画纸飞机组件

    纸飞机模拟一个物体在规定设计轴线偏离方位. //三角形 function DrawTriangle(canvas, A, B, C) { //画个三角形,“A.B.C”是顶点 with (canvas ...

  7. iOS 截屏分享(包含状态栏与不包含状态栏)

    iOS8以上的新方法PhotoKit 监听截图相册变化,取最后一张图片:http://www.hangge.com/blog/cache/detail_1515.html PhotoKit 获取本机相 ...

  8. poj2528 Mayor's posters【线段树】

    The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign h ...

  9. 浅谈jvm中的垃圾回收策略

    下面小编就为大家带来一篇浅谈jvm中的垃圾回收策略.小编觉得挺不错的,现在就分享给大家,也给大家做个参考.一起跟随小编过来看看吧   java和C#中的内存的分配和释放都是由虚拟机自动管理的,此前我已 ...

  10. CodeForces - 583D Once Again... LIS 循环

    题意:给你n个数(n<=100,ai<=300)  一个T,求n重复T次的最长非递减子序列. 题解:我直接考虑4 3 2 1 的例子,当其重复4次时会出现1 2 3 4 的子序列 ,此时再 ...