</>

110. Balanced Binary Tree

Easy NeetCode 150
Trees Time O(N) Space O(H)
↗ Open on LeetCode Source: NeetCode 150

Quick review

Pattern
Trees
Time
O(N)
Space
O(H)

Standard Solution (C++)

SummaryGiven a binary tree, determine whether it is height-balanced — meaning every node's left and right subtrees differ in height by at most one.

Approach
  • Use a single post-order DFS that computes the height of each subtree while simultaneously checking the balance condition.
  • The helper returns the actual height on success, or -1 as a sentinel to signal an imbalance anywhere in the subtree.
  • Because we propagate -1 upward immediately, no node is visited more than once, giving a true O(n) single-pass solution rather than the naive O(n log n) approach that recomputes heights redundantly.
  • The public method simply delegates to the helper and treats a non-negative result as balanced.
#include <algorithm>
#include <cstdlib>
using namespace std;

// TreeNode definition (provided by LeetCode judge)
struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* l, TreeNode* r) : val(x), left(l), right(r) {}
};

class Solution {
    // Returns the height of the subtree rooted at node,
    // or -1 if any subtree is found to be unbalanced.
    int height(TreeNode* node) {
        if (!node) return 0;

        int lh = height(node->left);
        if (lh == -1) return -1;          // early-exit: left side already broken

        int rh = height(node->right);
        if (rh == -1) return -1;          // early-exit: right side already broken

        if (abs(lh - rh) > 1) return -1; // this node violates the balance condition

        return max(lh, rh) + 1;
    }

public:
    bool isBalanced(TreeNode* root) {
        return height(root) != -1;
    }
};
Time O(n) Space O(h) — where h is the tree height (O(log n) balanced, O(n) worst-case skewed)

A canonical reference solution. Problem statement © LeetCode — see the official link above.

My Solution (C++)

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode() : val(0), left(nullptr), right(nullptr) {}
*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
    int checkHeight(TreeNode* root){
        if(!root){
            return 0;
        }
        int l=checkHeight(root->left);
        int r=checkHeight(root->right);
        if(l==-1 || r==-1 || std::abs(l-r)>1){
            return -1;
        }
        return std::max(l, r)+1;
    }
    bool isBalanced(TreeNode* root) {
        if(!root){
            return true;
        }
        return checkHeight(root)!=-1;
    }
};

// Time Complexity: O(N)
// Space Complexity: O(H) where H is the height of the tree, since we are using recursion (the call stack will take up space). 
// In the worst case (skewed tree), H = N. In the best case (balanced tree), H = log(N).
Time O(N) Space O(H)

My own solution and notes, as I wrote them.

▶ Run it — editable in-browser playground with a sample driver C++

Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.

C++ ▶ Runnable Auto-generated driver with sample input — unverified.

Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.

Related — same pattern