530. Minimum Absolute Difference in BST
来源:互联网 发布:9553软件下载 编辑:程序博客网 时间:2024/06/16 11:39
欢迎关注我的leetcode习题解答集,不断完善中,希望可以带给你帮助,共同进步
leetcode习题解答集
可以对照563. Binary Tree Tilt
- 超时了,思路是把树种的值全部取出来放在list中,然后处理
class Solution(object): def getMinimumDifference(self, root): """ :type root: TreeNode :rtype: int """ self.l = [] def get(node): if not node: pass else: self.l.append(node.val) get(node.left) get(node.right) get(root) print self.l min_c = 10000 for i in range(len(self.l)): for j in range(len(self.l)): if i != j: if abs(self.l[i]-self.l[j]) < min_c: min_c = abs(self.l[i]-self.l[j]) return min_c
- 不是很懂什么叫做BST,可以参考这篇文章BST树,原来这个树是已经排好序的;这样比较起来会快很多
class Solution(object): def getMinimumDifference(self, root): """ :type root: TreeNode :rtype: int """ def get(node): return get(node.left)+[node.val]+get(node.right) if node else [] min_c = 1000000 l = get(root) for i in range(len(l)-1): if min_c > abs(l[i]-l[i+1]): min_c = abs(l[i]-l[i+1]) return min_c
for i in range(len(l)-1): if min_c > abs(l[i]-l[i+1]): min_c = abs(l[i]-l[i+1]) return min_c
可以替换成
return min(abs(l[i]-l[i+1]) for i in range(len(l)-1))
这种精简的写法值得学习
0 0
- Leetcode-530. Minimum Absolute Difference in BST
- 【Leetcode】530. Minimum Absolute Difference in BST
- Leetcode 530. Minimum Absolute Difference in BST
- 530. Minimum Absolute Difference in BST
- 530. Minimum Absolute Difference in BST
- [leetcode] 530. Minimum Absolute Difference in BST
- 530. Minimum Absolute Difference in BST
- LeetCode-530. Minimum Absolute Difference in BST
- LeetCode 530. Minimum Absolute Difference in BST
- [LeetCode]530. Minimum Absolute Difference in BST
- 530. Minimum Absolute Difference in BST
- LeetCode | 530. Minimum Absolute Difference in BST
- leetcode 530. Minimum Absolute Difference in BST
- [leetcode]-530. Minimum Absolute Difference in BST
- 530. Minimum Absolute Difference in BST
- [LeetCode]530. Minimum Absolute Difference in BST
- 530. Minimum Absolute Difference in BST
- [leetcode]: 530. Minimum Absolute Difference in BST
- Hibernate(5)hibernate三种开发方式之官方推荐(但不常用)方式
- mysql实现避免重复插入
- 中国首个光量子计算机诞生了_来聊聊未来计算机
- SDNU 1370.Freckles 最小生成树 kruskal算法
- Geopandas 安装 (python中的地理空间数据)
- 530. Minimum Absolute Difference in BST
- Socks5协议中文文档(RFC1928)
- PCA算法(经典!!)
- nump.linspace使用
- 最靠谱的CDH5安装指南
- 脸书员工都在VR上工作了,你还等什么?
- 终极脑洞_你觉得这些科幻电影会成真吗?
- ElGamal公钥密码和椭圆曲线密码体制
- QT实现拖动没有标题栏的窗口