Leetcode - 110. Balanced Binary Tree
Coding Test

Leetcode - 110. Balanced Binary Tree



Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.


재귀로 구현.

class Solution:
    def isBalanced(self, root: TreeNode) -> bool:
        def get_height(node: TreeNode):
            if not node:
                return True, 1

            left_is_bal, left_hei = get_height(node.left)
            right_is_bal, right_hei = get_height(node.right)

            if abs(left_hei - right_hei) > 1:
                return False, max(left_hei, right_hei) + 1

            return left_is_bal and right_is_bal, max(left_hei, right_hei) + 1

        is_bal, _ = get_height(root)

        return is_bal