2019-08-24LeetCode110. 平衡二叉树

24min。百思不得其解,原来是depth1-depthr写成了is_left_balanced卧槽

class Solution:
    def isBalanced(self, root: TreeNode) -> bool:
        res,_=self.dfs(root)
        return res

    def dfs(self,root:TreeNode):
        if not root:return True,0
        is_left_balanced,depth1=self.dfs(root.left)
        is_right_balanced,depthr=self.dfs(root.right)
        is_balanced=True if abs(depth1-depthr)<2 and is_left_balanced and is_right_balanced else False
        return is_balanced,max(depth1,depthr)+1

你可能感兴趣的:(2019-08-24LeetCode110. 平衡二叉树)