How to Check if a Binary Tree Is Height-Balanced Using Postorder DFS
A binary tree is considered height-balanced when the difference between the left and right subtree heights is at most one for every node. A brute-force approach checks balance at each node separately, but repeatedly recomputes subtree heights, resulting in O(N²) time complexity. An optimised solution uses postorder DFS to compute height and detect imbalance in a single pass, returning -1 the moment any unbalanced subtree is found. This sentinel value propagates upward through the recursion, eliminating redundant calculations. The improved approach reduces time complexity to O(N) while maintaining O(H) space, where H is the height of the tree.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in