Balanced Tree
MediumWhen using a tree data structure, it's common for the tree to become unbalanced over time due to the insertion order of nodes, which can in turn affect the performance of our programs. Let's define a balanced tree as one where the difference in height of the left and right subtrees is at most one, for all nodes in the given tree. Write a function is_balanced(node) that determines whether a binary tree is balanced or not.
Input: The root node of a binary tree
Output: True if the tree is balanced, False otherwise.
Assume you are given the root node of a tree that conforms to the following interface:
class Node { left: Node right: Node value: any }
Examples
Example 1: Balanced
Tree: a / \ b c / \ \ d e f is_balanced(a) # => True
Example 2: Balanced
Tree: a / \ b c \ d is_balanced(a) # => True
Example 3: Not Balanced
Tree: a / \ b c \ d \ e is_balanced(a) # => False
Example 4: Not Balanced
Tree: a / \ b c / \ d e / \ f g is_balanced(a) # => False
Note that while the last tree seems symmetrical, it is not balanced because nodes b and c are not balanced.
This is a classic data structures question that really tests your understanding of concepts like trees, recursion, and algorithmic runtime. The quality of being "balanced" is actually very important in real-world applications—that's why we created self-balancing variants such as b-trees and red-black trees.
The key to solving this problem is to read the prompt and examples carefully. Let's begin with the definition: "a balanced tree is one where the difference in height of the left and right subtrees is at most one, for all nodes in the given tree." Off the bat, we know we need to compare the height of the left and right subtrees. We also know we need to do this comparison for all nodes in the tree. Now the question is, how do we get the height?
Let's start off by writing a function to get the height of a tree. If you aren't certain what the "height" is, you could ask your interviewer to clarify. The height of a tree is typically defined as the maximum number of 'layers' from top to bottom.
Now, let's use this to implement our main function, is_balanced. All we have to do is explore each node and make sure the height of its subtrees meet the condition in the prompt: the difference in height should be less than or equal to 1.
A note on recursion: Whenever you implement a recursive function, in the back of your mind you should ask whether recursion is the best way to accomplish your goal. Recursion has the downside of using the callstack, which can lead to stack overflow for deeply nested data. You're unlikely to hit this error during an interview, but it's worth mentioning as a real-life constraint and explaining how to implement a non-recursive version (e.g. by using a stack).
Time Complexity: . For each node, the get_height function is called to calculate the heights of the left and right subtrees. Since get_height is called for each of the n nodes in the worst case , this results in time complexity.
Space Complexity: . the space complexity is determined by the maximum depth of the recursion stack, which is the height of the tree (). Each recursive call of is_balanced adds to the call stack.
Optimized Approach
We can optimize the is_balanced function to reduce the time complexity to by combining the height calculation and balance checking into a single traversal. Here’s how:
Interview experiences
1 sharedRelated questions
Check if a given binary tree is height balanced.Related courses



