Skip to content

Latest commit

 

History

History
33 lines (31 loc) · 847 Bytes

File metadata and controls

33 lines (31 loc) · 847 Bytes

Lamarck      

0110 平衡二叉树


01 自底向上递归 o(N)

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def isBalanced(self, root):
        """
        :type root: Optional[TreeNode]
        :rtype: bool
        """
        def dfs(node):
            if node is None:
                return 0
            lh = dfs(node.left)
            if lh == -1:
                return -1
            rh = dfs(node.right)
            if rh == -1:
                return -1
            if abs(lh-rh) > 1:
                return -1
            return max(lh,rh) + 1
        return dfs(root) != -1