mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Merge pull request #1974 from ZerenZhang2022/patch-19
Update 0530.二叉搜索树的最小绝对差.md
This commit is contained in:
@ -221,8 +221,27 @@ class Solution:
|
||||
for i in range(len(res)-1): // 统计有序数组的最小差值
|
||||
r = min(abs(res[i]-res[i+1]),r)
|
||||
return r
|
||||
|
||||
|
||||
class Solution: # 双指针法,不用数组 (同Carl写法) - 更快
|
||||
def getMinimumDifference(self, root: Optional[TreeNode]) -> int:
|
||||
global pre,minval
|
||||
pre = None
|
||||
minval = 10**5
|
||||
self.traversal(root)
|
||||
return minval
|
||||
|
||||
def traversal(self,root):
|
||||
global pre,minval
|
||||
if not root: return None
|
||||
self.traversal(root.left)
|
||||
if pre and root.val-pre.val<minval:
|
||||
minval = root.val-pre.val
|
||||
pre = root
|
||||
self.traversal(root.right)
|
||||
```
|
||||
|
||||
|
||||
迭代法-中序遍历
|
||||
```python
|
||||
class Solution:
|
||||
|
Reference in New Issue
Block a user