/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(10, TreeNode(5), TreeNode(15))

Output: True

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

Example 2

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

Output: False

Explanation: Left subtree has height 3, right subtree has height 0, difference is 3.

Example 3

Input: None

Output: True

Explanation: An empty tree is balanced.

Constraints

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

Case 1

Input: TreeNode(7, TreeNode(3, None, TreeNode(4)), TreeNode(8))

Expected: True

Case 2

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

Expected: False

Case 3

Input: TreeNode(5, None, TreeNode(6, None, TreeNode(7)))

Expected: False

Case 4

Input: TreeNode(9, TreeNode(8), None)

Expected: True