Advertisement
Guest User

Maximum Difference Between Node and Ancestor

a guest
Mar 10th, 2021
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. def maxAncestorDiff(root: TreeNode) -> int:
  2. if root is None:
  3. return 0
  4.  
  5. def _maxAncestorDiff(root, curMin, curMax):
  6. if root is None:
  7. return curMax - curMin
  8.  
  9. curMin = min(root.val, curMin)
  10. curMax = max(root.val, curMax)
  11.  
  12. leftTree = _maxAncestorDiff(root.left, curMin, curMax)
  13. rightTree = _maxAncestorDiff(root.right, curMin, curMax)
  14.  
  15. return max(leftTree, rightTree)
  16.  
  17. return _maxAncestorDiff(root, root.val, root.val)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement