leetcode-mid-Linked list- 230 Kth Smallest Element in a BST
mycode 81.40%
# 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
"""
self.res = []
def inorder(root):
if not root:
return
inorder(root.left)
self.res.append(root.val)
inorder(root.right)
inorder(root)
return self.res[k-1]
参考
思路:
我的方法中先将整个tree都遍历了一遍,其实是不必要的,那么该如何恰好在找到第k个数的时候及时退出呢-》
def kthSmallest(root, k):
"""
:type root: TreeNode
:type k: int
:rtype: int
"""
s = []
p = root
cnt = 0
print('p',p)
while s or p:
if p:
s.append(p)
print('if',p.val,s)
p = p.left
else:
p = s[-1].right
cnt += 1 #上面之所以能取出p是因为已经没有左子树了,所以最后左子树的叶子就是目前数里面最小的数,计数+1
print('else',s)
if cnt == k:
return s[-1].val
s.pop()
leetcode-mid-Linked list- 230 Kth Smallest Element in a BST的更多相关文章
- [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 ...
- [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 ...
- 【LeetCode】230. Kth Smallest Element in a BST
Difficulty: Medium More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/kth-smallest- ...
- (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 ...
- [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 ...
- LeetCode OJ 230. Kth Smallest Element in a BST
Total Accepted: 46445 Total Submissions: 122594 Difficulty: Medium Given a binary search tree, write ...
- 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. Not ...
随机推荐
- easy-mock的运用
一.概念 Easy Mock 是杭州大搜车无线团队出品的一个极其简单.高效.可视化.并且能快速生成模拟数据的 在线 mock 服务 .以项目管理的方式组织 Mock List,能帮助我们更好的管理 ...
- P1076寻宝
---恢复内容开始--- 这是2012noip普及组的一个模拟题,第一次得了50,看了题解后剪枝拿到100. N层楼,m个房间,逆时针排序.每个房间有一个指示牌,也可能有楼梯,找到第(上楼的第一个房间 ...
- 【模板】最长上升子序列(LIS)及其优化 & 洛谷 AT2827 LIS
最长上升子序列 传送门 题意 对于给定的一个n个数的序列,找到它的一个最长的子序列,并且保证这个子序列是由低到高排序的. 例如,1 6 2 5 4 6 8的最长上升子序列为1 2 4 6 8. 基本思 ...
- django学习笔记(五)
知识点概要 - Session - CSRF - Model操作 - Form验证(ModelForm) - 中间件 - 缓存 - 信号 内容详细: 1. Session 基于Cookie做用户验证时 ...
- 简单的物流项目实战,WPF的MVVM设计模式(一)
新建一个WPF项目,命名为WMS 然后分别新建文件夹,Data,Models,Views,ViewModels,Services,如下图所示 然后通过NuGet安装连个Nuget包,分别为SQLite ...
- apachectl 命令详解-graceful 不中断原有连接,重新启动 Apache 服务器
apachectl(Apache control interface) 参 数: fullstatus 显示服务器完整的状态信息. graceful 重新启动 Apac ...
- CentOS7升级gcc
CentOS7.5升级gcc到8.3.0版本 1.下载源码包 cd /usr/local/src wget http://ftp.tsukuba.wide.ad.jp/software/gcc/rel ...
- PAT Advanced 1007 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- libevent cs
int evutil_make_listen_socket_reuseable(evutil_socket_t sock): 相当于执行以下操作 int one = 1; setsockopt(soc ...
- qt01 lineEdit pushButton
1. void udp_server::on_lineEdit_textEdited() { ui->pushButton->setEnabled(ui->lineEdit-> ...