前段时间一直在弄毕设的事情,但是心里一直记着要刷leetcode,昨天调得差不多了,今天!在吃完了一个油饼之后!终于!打开了leetcode & csdn!哈哈哈哈,感觉重新掌控了天下
回归正题,深度优先遍历?
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def maxAncestorDiff(self, root, mn = 100000, mx = 0):
"""
:type root: TreeNode
:rtype: int
"""
return max(self.maxAncestorDiff(root.left, min(mn, root.val), max(mx, root.val)),\
self.maxAncestorDiff(root.right, min(mn, root.val), max(mx, root.val)))\
if root else mx - mn