-
Notifications
You must be signed in to change notification settings - Fork 177
/
1026.py
25 lines (24 loc) · 815 Bytes
/
1026.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def maxAncestorDiff(self, root: TreeNode) -> int:
self.res = 0
def dfs(node):
mx = mn = node.val
if node.left:
lMn, lMx = dfs(node.left)
mx = max(mx, lMx)
mn = min(mn, lMn)
self.res = max(self.res, abs(lMn - node.val), abs(lMx - node.val))
if node.right:
rMn, rMx = dfs(node.right)
mx = max(mx, rMx)
mn = min(mn, rMn)
self.res = max(self.res, abs(rMn - node.val), abs(rMx - node.val))
return mn, mx
dfs(root)
return self.res