leetcode -- Balanced Binary Tree -- 简单重点

https://leetcode.com/problems/balanced-binary-tree/

求depth,recursive就行


class Solution(object):

    def depth(self, root):

        if not root:
            return 0

        return max(self.depth(root.left), self.depth(root.right)) + 1

    def isBalanced(self, root):
        """ :type root: TreeNode :rtype: bool """
        if not root : return True
        return abs(self.depth(root.left) - self.depth(root.right)) <= 1 and self.isBalanced(root.left) and self.isBalanced(root.right)

你可能感兴趣的:(leetcode -- Balanced Binary Tree -- 简单重点)