Answers for "check if binary tree is balanced python"

0

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)
Posted by: Guest on February-25-2022

Code answers related to "check if binary tree is balanced python"

Python Answers by Framework

Browse Popular Code Answers by Language