Leetcode - 110. Balanced Binary Tree
문제
풀이
재귀로 구현.
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