Balanced Binary Tree

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

Example 1
        3
       / \
      9   20
         /  \
        15   7
Inputroot = [3,9,20,null,null,15,7]
Outputtrue
The given tree is height-balanced, so the result is true.
Example 2
        1
       / \
      2   2
     / \
    3   3
   / \
  4   4
Inputroot = [1,2,2,3,3,null,null,4,4]
Outputfalse
The given tree is not height-balanced, so the result is false.

Constraints

  • The number of nodes in the tree is in the range [0, 5000].
  • -10^4 <= Node.val <= 10^4

Asked at 7 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate