We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent ce06bc7 commit ae5e710Copy full SHA for ae5e710
1001-1500/1026.py
@@ -0,0 +1,22 @@
1
+# Definition for a binary tree node.
2
+# class TreeNode:
3
+# def __init__(self, val=0, left=None, right=None):
4
+# self.val = val
5
+# self.left = left
6
+# self.right = right
7
+class Solution:
8
+ def maxAncestorDiff(self, root: Optional[TreeNode]) -> int:
9
+ maxVal = 0
10
+
11
+ queue = deque([(root, root.val, root.val)])
12
+ while queue:
13
+ curr, high, low = queue.popleft()
14
+ currVal = curr.val
15
+ diff1, diff2 = currVal - low, high - currVal
16
+ maxVal = max(maxVal, diff1, diff2)
17
18
+ if curr.left:
19
+ queue.append((curr.left, max(currVal, high), min(currVal, low)))
20
+ if curr.right:
21
+ queue.append((curr.right, max(currVal, high), min(currVal, low)))
22
+ return maxVal
0 commit comments