Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 37d12a1

Browse files
Update 0530.二叉搜索树的最小绝对差.md
增加python - 双指针法,不用数组 (同Carl写法) - 更快
1 parent 76fd68c commit 37d12a1

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,27 @@ class Solution:
221221
for i in range(len(res)-1): // 统计有序数组的最小差值
222222
r = min(abs(res[i]-res[i+1]),r)
223223
return r
224+
225+
226+
class Solution: # 双指针法,不用数组 (同Carl写法) - 更快
227+
def getMinimumDifference(self, root: Optional[TreeNode]) -> int:
228+
global pre,minval
229+
pre = None
230+
minval = 10**5
231+
self.traversal(root)
232+
return minval
233+
234+
def traversal(self,root):
235+
global pre,minval
236+
if not root: return None
237+
self.traversal(root.left)
238+
if pre and root.val-pre.val<minval:
239+
minval = root.val-pre.val
240+
pre = root
241+
self.traversal(root.right)
224242
```
225243

244+
226245
迭代法-中序遍历
227246
```python
228247
class Solution:

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /