Balanced Binary Tree

Problem

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

Pseudocode

Solution

Time and Space Complexity

Time

  • Recurse to find the max depth and compare the max height of left and right subtrees. Scales linearly with input size all nodes are visited. O(N)

  • Total - O(N)

Space

  • Memory requirements of recursive stack also scales linearly to input. O(N)

  • Total - O(N)

Last updated