mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
Merge pull request #182 from jojoo15/patch-12
添加 0530.二叉搜索树的最小绝对差 python版本
This commit is contained in:
@ -177,8 +177,29 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
|
||||
```python
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
# self.val = val
|
||||
# self.left = left
|
||||
# self.right = right
|
||||
class Solution:
|
||||
def getMinimumDifference(self, root: TreeNode) -> int:
|
||||
res = []
|
||||
r = float("inf")
|
||||
def buildaList(root): //把二叉搜索树转换成有序数组
|
||||
if not root: return None
|
||||
if root.left: buildaList(root.left) //左
|
||||
res.append(root.val) //中
|
||||
if root.right: buildaList(root.right) //右
|
||||
return res
|
||||
|
||||
buildaList(root)
|
||||
for i in range(len(res)-1): // 统计有序数组的最小差值
|
||||
r = min(abs(res[i]-res[i+1]),r)
|
||||
return r
|
||||
```
|
||||
Go:
|
||||
|
||||
|
||||
@ -188,4 +209,4 @@ Go:
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||
|
Reference in New Issue
Block a user