Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

check if binary tree is balanced python

def isBalanced(self, root: Optional[TreeNode]) -> bool:
        def depth(root):
            if not root:
                return 0
            return max(depth(root.left), depth(root.right)) + 1
        def solution(root):
            if not root:
                return True
            if abs(depth(root.left) - depth(root.right)) > 1:
                return False
            return solution(root.left) and solution(root.right)
        return solution(root)
 
PREVIOUS NEXT
Tagged: #check #binary #tree #balanced #python
ADD COMMENT
Topic
Name
7+7 =