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.
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?
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def kthSmallest(self, root, k):
"""
:type root: TreeNode
:type k: int
:rtype: int
"""
# left first
node = root
nodes = []
n = 0
while node:
nodes.append(node)
node = node.left
while nodes:
node = nodes.pop() # min val
n += 1
if n == k:
return node.val
node = node.right
while node:
nodes.append(node)
node = node.left
return None
230. Kth Smallest Element in a BST ——迭代本质:a=xx1 while some_condition: a=xx2的更多相关文章
- [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 (2 solutions)
Kth Smallest Element in a BST Given a binary search tree, write a function kthSmallest to find the k ...
- 【刷题-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 ...
- (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 ...
- 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 i ...
- [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
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 ...
随机推荐
- NoSQL数据库的分布式模型
NoSQL数据库的分布式模型 单一服务器 在一个服务器完全能够胜任工作时就没必要考虑分布式,这样部署简单,维护也方便很多: 分片 特点 数据的各个部分存放在集群的不同服务器中: 比如按字母来划分:以a ...
- css3 3d学习笔记
几个属性: -webkit-perspective:0;景深(默认0), -webkit-perspective-origin:center center;景深基点(默认中间), -webkit-tr ...
- JMS【一】--JMS基本概念
摘要:The Java Message Service (JMS) API is a messaging standard that allows application components bas ...
- 如何写好CSS?(OOCSS\DRY\SMACSS)
我现在面对的CSS基本上就是一个三头六臂的怪物,一点不夸张,因为真的是三头六臂,同一个样式在同一个element上作用了好几遍,而同一个样式又分散在4,5个class上,优先级有很多层.可以看得出这个 ...
- iOS - Swift 面向对象语法
1.面向对象 面向过程:面向过程编成将所要解决的问题按解决问题的步骤进行分析.如果是大问题,就分解成为多个不同的小问题,在程序里叫做划分成不同的模块.每一个解决的步骤可能是一行或者几行代码,也可能是一 ...
- ssis freach loop container 传入变量给 某些数据源的时候。
ssis freach loop container 传入变量给 某些数据源的时候. 应该选择loop container ,设置delayvalidateion为true. 这样数据源控件就不会报e ...
- 初试Celery
从@到celery 一.文档: 官网:http://www.celeryproject.org/ Celery3.1 ------------2016-7-19 18:26:55-- source:[ ...
- PL/SQL 编写规范
大小写风格 所有数据库关键字和保留字均大写; 缩进风格 使用空格, 缩进2格(前边空两个格, 后边要与SQL语句的一致, 比如下面:) SELECT * FROM TABLE 空格及换行 1行只写1条 ...
- Linux命令之nslookup
http://www.computerhope.com/unix/unslooku.htm About nslookup The nslookup command is used to query i ...
- 如何将根文件系统制作成yaffs格式,并设置从yaffs启动
1.利用mkyaffs2image 工具,将根文件系统打包成yaffs镜像包 mkyaffs2image-128M root_qtopia root_qtopia.img 2.设置uboot参数boo ...