Medium Data Structures Trees & Binary Trees
How do you check if a binary tree is balanced?
Answer
A balanced binary tree has the height difference between left and right subtrees of every node at most 1. The optimal O(n) solution uses a bottom-up approach: recursively compute height while checking balance, returning -1 if imbalanced. For each node, get heights of left and right subtrees; if either is -1 or their difference exceeds 1, return -1; otherwise return max(left, right) + 1.
IIT Certified
Master These Concepts with IIT Certification
175+ hours of industry projects. Get placed at Bosch, Tata Motors, L&T and 500+ companies.
Relevant for Roles
Software Engineer Backend Developer Algorithm Developer