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 ...
随机推荐
- Get started with Gazebo in ROS
Run Gazebo These three steps will run Gazebo with a default world. Install Gazebo. Open a terminal. ...
- POJ 1321棋盘问题
题意: 给定一个n*n(n<=8)的棋盘, 有些地方能放棋子, 有些不能. 放m个棋子, 求能使这m个棋子不同行且不同列的方案数. 分析: 用一个一维数组标记行和列, 深搜一下. #includ ...
- 异步上传图片,光用jquery不行,得用jquery.form.js插件
异步上传图片,光用jquery不行,得用jquery.form.js插件,百度一下下载这个插件,加jquery,引入就可以了 <form id="postbackground" ...
- redis系列
1.redis windows下的环境搭建 2.redis初学系列
- Team Formation
#include <stdio.h> #include <ctype.h> #include <math.h> #include <stack> #in ...
- CXF WebService 开发文档
参考资料: 1. http://cxf.apache.org/docs/ 2. http://www.cnblogs.com/hoojo/archive/2011/03/30/1999587.html ...
- [转载] 深入理解 docker ulimit
深入理解docker ulimit 2015年7月23日 10:00 阅读 12778 [编者的话]Docker大规模应用后,如果你没踩过坑,说出去肯定没人信.昨天就遇到一个ulimit的经典问题:业 ...
- maven之详解继承与聚合
说到聚合与继承我们都很熟悉,maven同样也具备这样的设计原则,下面我们来看一下Maven的pom如何进行聚合与继承的配置实现. 一.为什么要聚合? 随着技术的飞速发展和各类用户对软件的要求越来越高, ...
- python paramiko模块SSH自动登录linux系统进行操作
1). Linux系统首先要开启SSH服务:service ssh status 如果没安装的话,则要:apt-get install openssh-server service ssh resta ...
- The Zen Programmer (zhuan)
http://blog.csdn.NET/marksinoberg/article/details/52460725 ***************************************** ...