Prepin
Log in

Determine whether the provided binary tree is height-balanced.

Approach / Explanation

A height-balanced binary tree is characterized as a binary tree where the depths of the left and right subtrees of each node do not differ by more than one. In this task, you are required to determine whether a given binary tree is height-balanced. To solve this problem, it is essential to grasp the concept of a binary tree, the methods for traversing such structures, and the definition of a height-balanced tree. One effective strategy may involve utilizing a helper function to consistently return the height of the tree. Consider the following important points: You must traverse every node. For each node, verify that both its left and right subtrees are balanced. Apply the property of height-balanced trees, which states that for each node, the height difference between its left and right subtrees should not exceed 1.

Suggested Answer

One potential solution is as follows: For each node, we determine the heights of the left and right subtrees and verify if the absolute difference between these heights exceeds one. This method can be implemented using a recursive function that calculates the height of the binary tree if it is balanced, returning -1 if it is not height-balanced. Consequently, the function will yield a non-negative height when the tree is balanced and a negative value when it is not. class TreeNode { int val ; TreeNode left ; TreeNode right ; } public boolean isBalanced ( TreeNode root ) { return height ( root ) != - 1 ; } public int height ( TreeNode root ) { if ( root == null ) { return 0 ; } int left = height ( root . left ) ; int right = height ( root . right ) ; if ( left == - 1 || right == - 1 || Math . abs ( left - right ) > 1 ) { return - 1 ; } return Math . max ( left , right ) + 1 ; }

Alternative Answer

Another method involves utilizing a Depth-First Search (DFS) approach. We begin at the root and recursively determine the height of both the left and right subtrees for each node. class TreeNode ( object ) : def __init__ ( self , x ) : self . val = x self . left = None self . right = None def isBalanced ( self , root ) : def check ( root ) : if root is None : return 0 left = check ( root . left ) right = check ( root . right ) if left == - 1 or right == - 1 or abs ( left - right ) > 1 : return - 1 return 1 + max ( left , right ) return check ( root ) != - 1 This Python code defines the isBalanced method along with a helper function called check , which assesses whether the tree rooted at a given node is height-balanced. We implement a post-order traversal to verify the balance of the left subtree first, followed by the right subtree, and finally the node itself. If we discover that any subtree is unbalanced during the traversal, we return -1, ceasing further checks, which leads to the conclusion that the entire tree is not balanced.

Question Details

Difficulty & Category

Medium

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.