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 ...
随机推荐
- 安装libpng库
一. 安装libpng库前需要先安装zlib库,libpng库依赖zlib库 1.1. zlib库安装 1.1.1. 下载地址:http://www.zlib.net/ 1.1.2. 解压后得到zli ...
- [多校联考2019(Round 5 T3)]青青草原的表彰大会(dp+组合数学)
[多校联考2019(Round 5)]青青草原的表彰大会(dp+组合数学) 题面 青青草原上有n 只羊,他们聚集在包包大人的家里,举办一年一度的表彰大会,在这次的表彰大会中,包包大人让羊们按自己的贡献 ...
- Linux菜狗入门(不停更新)
资料来源:<腾讯课堂> 1, 计算机硬件包括CPU,内存,硬盘,声卡等等 2, 没有安装操作系统的计算机,通常被称为裸机 如果想在裸机上运行自己所编写的程序,就必须用机器语言书写程序 如果 ...
- nodejs版实现properties后缀文件解析
1.propertiesParser.js let readline = require('readline'); let fs = require('fs'); // properties文件路径 ...
- C# Excel数据验重及Table数据验重
http://blog.csdn.net/jiankunking/article/details/38398087 最近在做导入Excel数据的时候,要检验数据是否重复: 1.要检验Excel数据本身 ...
- 移动前端不得不了解的Meta标签
http://ghmagical.com/article/page/id/PSeJR0rPd34k
- Antd时间选择框汉化问题总结------国际化全局设置
import zh_CN from 'antd/lib/locale-provider/zh_CN'; import 'moment/locale/zh-cn'; import { ConfigPro ...
- 使用css3的repeating-linear-gradient画虚线
还在用 border-style: dashed 画虚线吗?虽然也是虚线,但是不能控制每一个虚线的宽度 .dashed { height: 1px; background-image: repeati ...
- 封装自己的framework静态库
ios中我们写的代码有时不愿意让别人看到,可能对它进行封装,生成一个静态库如典型的.a,还有一种就是和 苹果自带的库一样的后缀名为.framework的库,个人推荐使用.framework,因为.a不 ...
- 生成对抗网络 Generative Adversarial Networks
转自:https://zhuanlan.zhihu.com/p/26499443 生成对抗网络GAN是由蒙特利尔大学Ian Goodfellow教授和他的学生在2014年提出的机器学习架构. 要全面理 ...