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.

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?

本题和不用递归的方法中序遍历BST (Leetcode 94题) 基本上一样。我们只要在iteratively的过程中输出第K个val就可以了。

  1. class Solution(object):
  2. def kthSmallest(self, root, k):
  3. """
  4. :type root: TreeNode
  5. :type k: int
  6. :rtype: int
  7. """
  8. ans = []
  9. stack = []
  10.  
  11. while stack or root:
  12. while root:
  13. stack.append(root)
  14. root = root.left
  15. root = stack.pop()
  16. ans.append(root.val)
  17. if len(ans) == k:
  18. return ans[-1]
  19. root = root.right
  20. return ans[-1]

Leetcode 230. Kth Smallest Element in a BST的更多相关文章

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

  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. Java for LeetCode 230 Kth Smallest Element in a BST

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

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

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

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

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

  8. 【LeetCode】230. Kth Smallest Element in a BST (2 solutions)

    Kth Smallest Element in a BST Given a binary search tree, write a function kthSmallest to find the k ...

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

    Kth Smallest Element in a BST Given a binary search tree, write a function kthSmallest to find the k ...

随机推荐

  1. js判断鼠标是否停止移动

    本程序实现当鼠标在一个特定的div内悬停n秒时,判断出已经停止移动. 思路: 1.定义全局变量鼠标移动状态imouse,定时器timer.当鼠标在div内移动时,imouse值为1,相反静止时值为0: ...

  2. C#:委托和自定义事件

    1. 委托概述 “委托”相当于C++中的“函数指针”,委托必须与所要“指向”的函数在“参数”和“返回类型”上保持一致; // 定义Person类 public class Person { publi ...

  3. openstack-swift云存储部署(二)

    接上篇,swift-proxy和swift-store的安装 先说一下服务器分配 swift-proxy和keystone部署在192.168.25.11 swift-store是两台  分别是192 ...

  4. Java日志框架:SLF4J,Common-Logging,Log4J,Logback说明

    Log4j  Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件.甚至是套接口服务 器.NT的事件记录器.UNIX Syslog守护进程等 ...

  5. MSSQL复制中的发布与订阅

    准备条件 1.2台服务器 2.WINDOWS SERVER 2008 64bit + 3.SQL SERVER 2008 R2 + 4.MSSQLSERVER服务与MSSQLAGENT服务正常运行中 ...

  6. 断言(Assert)与异常(Exception)

    断言是被用来检查非法情况而不是错误情况,即在该程序正常工作时绝不应该发生的非法情况,用来帮助开发人员对问题的快速定位.异常处理用于对程序发生异常情况的处理,增强程序的健壮性.容错性,减少程序使用中对用 ...

  7. 浅谈Service层为何要有接口

    被人随意问了一句,为何每个service层都要写一个接口呢,多麻烦~虽然想说点什么,但是又不知道从何说起,只好从新整理一下思绪. 情景1:在开源框架中有很多这种情况,就是某个功能支持用户自定义扩展.说 ...

  8. 【小白的CFD之旅】05 补充基础

    黄师姐是一个很干脆果敢的人,从她的日常装扮就能显露出来.卡帕运动装,白色运动鞋,马尾辫,这是小白对黄师姐的第一印象.“明天早上九点钟来实验室,我给你安排这阵子的任务.”黄师姐对小白说.说话语气和老蓝一 ...

  9. pacemaker+corosync/heartbeat对比及资源代理RA脚本

    一.Pacemaker概念 (1)Pacemaker(心脏起搏器),是一个高可用的群集资源管理器.它实现最大可用性资源管理的节点和资源级故障检测和恢复,通过使用首选集群基础设施(Corosync或He ...

  10. 《python核心编》程课后习题——第三章

    核心编程课后习题——第三章 3-1 由于Python是动态的,解释性的语言,对象的类型和内存都是运行时确定的,所以无需再使用之前对变量名和变量类型进行申明 3-2原因同上,Python的类型检查是在运 ...