Merge branch 'youngyangyang04:master' into master

This commit is contained in:
VvV
2023-04-02 21:28:52 +08:00
committed by GitHub

View File

@ -221,8 +221,27 @@ class Solution:
for i in range(len(res)-1): // 统计有序数组的最小差值 for i in range(len(res)-1): // 统计有序数组的最小差值
r = min(abs(res[i]-res[i+1]),r) r = min(abs(res[i]-res[i+1]),r)
return 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 ```python
class Solution: class Solution: