Skip to content

Commit 4f96f64

Browse files
authored
Update 0530.二叉搜索树的最小绝对差.md
added python version of code
1 parent cac4686 commit 4f96f64

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

problems/0530.二叉搜索树的最小绝对差.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,29 @@ class Solution {
177177
```
178178

179179
Python:
180-
181-
180+
```python
181+
# Definition for a binary tree node.
182+
# class TreeNode:
183+
# def __init__(self, val=0, left=None, right=None):
184+
# self.val = val
185+
# self.left = left
186+
# self.right = right
187+
class Solution:
188+
def getMinimumDifference(self, root: TreeNode) -> int:
189+
res = []
190+
r = float("inf")
191+
def buildaList(root): //把二叉搜索树转换成有序数组
192+
if not root: return None
193+
if root.left: buildaList(root.left) //
194+
res.append(root.val) //
195+
if root.right: buildaList(root.right) //
196+
return res
197+
198+
buildaList(root)
199+
for i in range(len(res)-1): // 统计有序数组的最小差值
200+
r = min(abs(res[i]-res[i+1]),r)
201+
return r
202+
```
182203
Go:
183204

184205

@@ -188,4 +209,4 @@ Go:
188209
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
189210
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
190211
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
191-
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
212+
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>

0 commit comments

Comments
 (0)