Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

root =

Codey

Practise coding problems, test your solutions and track your progress.

Explore

  • Problems
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 Codey. Personal learning project.

Solution
Updated: 2026-02-23

Idea

Compute heights bottom-up and stop early if any subtree is unbalanced.

Approach

DFS returns subtree height, or -1 to signal imbalance. A node is unbalanced if child heights differ by more than 1.

Why it works

Every subtree’s balance depends only on its children’s heights and balance status.

Code
Loading...
Complexity
Time: O(n)
Space: O(h)
Solution
Updated: 2026-02-23

Idea

Compute heights bottom-up and stop early if any subtree is unbalanced.

Approach

DFS returns subtree height, or -1 to signal imbalance. A node is unbalanced if child heights differ by more than 1.

Why it works

Every subtree’s balance depends only on its children’s heights and balance status.

Code
Loading...
Complexity
Time: O(n)
Space: O(h)