Skip to content

Latest commit

 

History

History
49 lines (40 loc) · 1.15 KB

110_balanced_binary_tree.md

File metadata and controls

49 lines (40 loc) · 1.15 KB

Given a binary tree, determine if it is height-balanced.

Example 1:

Input: root = [3,9,20,null,null,15,7]
Output: true

Example 2:

Input: root = [1,2,2,3,3,null,null,4,4]
Output: false

Example 3:

Input: root = []
Output: true

Solution

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
        
class Solution:
    def height(self, root):
        if root is None:
            return 0
        return max(self.height(root.left), self.height(root.right)) + 1

    def isBalanced(self, root: Optional[TreeNode]) -> bool:
        if root is None:
            return True
        left_height = self.height(root.left)
        right_height = self.height(root.right)

        if abs(left_height - right_height) <= 1 and self.isBalanced(root.left) and self.isBalanced(root.right):
            return True
        return False