/110. Balanced Binary Tree

110. Balanced Binary Tree

Easy
Trees58% acceptance

Given the root node of a binary tree, determine whether the tree is height-balanced. A binary tree is height-balanced if for every node, the difference in height between its left and right subtrees is at most 1.

Example 1

Input: TreeNode(5, TreeNode(3), TreeNode(8))

Output: True

Explanation: Both subtrees have height 1, difference is 0.

Example 2

Input: TreeNode(1, TreeNode(2, TreeNode(3)), None)

Output: False

Explanation: Left subtree height is 2, right is 0, difference is 2.

Example 3

Input: None

Output: True

Explanation: Empty tree is balanced.

Constraints

  • 0 <= number of nodes <= 5000
  • -10^4 <= node value <= 10^4
Python (current runtime)

Case 1

Input: TreeNode(10, TreeNode(5, TreeNode(2), None), TreeNode(15))

Expected: True

Case 2

Input: TreeNode(7, TreeNode(4, TreeNode(2, None, TreeNode(1)), None), TreeNode(9))

Expected: False

Case 3

Input: TreeNode(0)

Expected: True

Case 4

Input: TreeNode(6, None, TreeNode(8, None, TreeNode(10)))

Expected: False