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 ...
随机推荐
- CGAffineTransform与CATransform3D
CGAffineTransform 1.CG的前缀告诉我们,CGAffineTransform类型属于Core Graphics框架,Core Graphics实际上是一个严格意义上的2D绘图API, ...
- 线程入门之join方法
package com.thread; /** * <join:将某线程加入进来,相当于方法调用,也叫合并某个线程> * <功能详细描述> * * @author 95Yang ...
- tilemap坐标转换
像素点跟tile的索引之间的转换//从cocos2d-x坐标转换为Tilemap坐标CCPoint GameMap::tileCoordForPosition(CCPoint position){ i ...
- 数据库分库分表(sharding)
地址: http://blog.csdn.net/column/details/sharding.html
- DOM解析XML练习
首先以XML文件存储数据,格式如下(作为数据库) exam.xml <?xml version="1.0" encoding="UTF-8" standa ...
- box-shadow使用指南
Summary The box-shadow property describes one or more shadow effects as a comma-separated list. It e ...
- andriod之摄像头驱动流程
camera成像原理: 景物通过镜头生产光学图像投射到sensor表面上,然后转为模拟电信号,经过数模变成数字图像信号,在经过DSP加工出来,然后在通过IO接口传输到CPU处理. 由于摄像头满足总线. ...
- Python学习(12)日期和时间
目录 Python 日期和时间 时间元组 获取当前时间 获取格式化时间 格式化日历 获取某月日历 Time模块 日历模块 其他相关模块和函数 Python 日期和时间 Python 程序能用很多方式处 ...
- xutils使用过程
1,导入xutils的jar包 2,添加xutils需要使用的权限 <uses-permission android:name="android.permission.INTERNET ...
- Android ViewPager更新数据
ViewPager也是一个常用的组件 与ListView类似 当绑定数据后 想刷新数据 需要在适配器中添加如下方法 protected PagerAdapter galleryAdapter = ne ...