Introduction
In computer science, a balanced binary tree is a tree where the height difference between the left and right subtrees of any node is at most 1. This property ensures optimal performance for tree operations like search, insert, and delete. This article explains how to determine if a binary tree is balanced using a depth-first search (DFS) approach.
Problem Statement
Given the root of a binary tree, determine if it is height-balanced. A binary tree is height-balanced if:
The left and right subtrees of every node differ in height by no more than 1.
Both the left and right subtrees are also height-balanced.
Example
Input:
3
/ \
9 20
/ \
15 7
Output:
True
Approach
The solution employs a DFS approach to traverse the binary tree and calculate both the height and balance status of each subtree recursively.
Steps:
Base Case:
- If the node is
None
, return[True, 0]
(balanced and height 0).
- If the node is
Recursive Case:
Calculate the balance status and height for the left and right subtrees.
Check if the current node is balanced by:
Ensuring both subtrees are balanced.
Verifying that the height difference between the left and right subtrees is at most 1.
Return the balance status and the height of the current node.
Result:
- The root node’s balance status determines whether the entire tree is balanced.
Complexity
Time Complexity: O(n)
- Each node is visited once during the traversal.
Space Complexity: O(h)
- The recursion stack depth depends on the height of the tree, where h is the tree's height.
Python Code
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
def dfs(root):
if not root:
return [True, 0]
left = dfs(root.left)
right = dfs(root.right)
balanced = left[0] and right[0] and abs(left[1] - right[1]) <= 1
return [balanced, 1 + max(left[1], right[1])]
return dfs(root)[0]
Example Walkthrough
Input Tree:
3
/ \
9 20
/ \
15 7
Execution:
Starting at
root (3)
, calculate the balance and height for both subtrees:Left subtree (
9
): Balanced with height 1.Right subtree (
20
): Balanced with height 2.
Compare the heights of the subtrees:
- Difference =
|2 - 1| = 1
, which satisfies the balance condition.
- Difference =
Return
True
for the root node.
Output:
True
Advantages of DFS for Tree Balance Checks
Efficiency: Combines height calculation and balance checking into a single traversal.
Simplicity: The recursive approach mirrors the tree's structure, making it intuitive and concise.
Conclusion
The DFS-based solution for checking if a binary tree is balanced is both efficient and elegant. By ensuring that the height and balance of each subtree are computed together, the algorithm avoids redundant calculations.
If you found this article helpful, feel free to explore more binary tree problems and solutions!
Let’s Connect!
If you enjoyed this article, feel free to connect with me on LinkedIn and check out more of my articles on Hashnode!