0% completed
Problem Statement
Determine if a binary tree is height-balanced.
A binary tree is considered height-balanced if, for each node, the difference in height between its left and right subtrees is no more than one.
Examples:
- Input:
3
/ \
9 20
/ \
15 7
- Expected Output: true
- Justification: Every node in the tree has a left and right subtree depth difference of either 0 or 1.
- Input:
1
/ \
2 2
/ \ / \
3 3 3 3
/ \ / \
4 4 4 4
- Expected Output: true
- Justification: Each node in the tree has a left and right subtree depth difference of either 0 or 1.
- Input:
1
/ \
2 5
/
3
/
4
- Expected Output: false
- Justification: The root node has a left subtree depth of 3 and right subtree depth of 1. The difference (3 - 1 = 2) exceeds 1, hence the tree is not balanced.
Constraints:
- The number of nodes in the tree is in the range
[0, 5000]
. - -10<sup>4</sup> <= Node.val <= 10<sup>4</sup>
Solution
To check if a binary tree is balanced, an efficient approach involves a depth-first traversal of the tree. Starting from the root, recursively compute the height of both the left and right subtrees. If at any node, the height difference between its left and right subtree exceeds one, the tree is imbalanced. Otherwise, proceed to check its child nodes. The height of a node is the maximum height of its left or right subtree plus one. This way, we can efficiently traverse each node once and check the height difference of the left and right subtree.
- Base Case: If the node is null, return a depth of 0.
- Recursive Case: Recursively calculate the depth of the left and right subtrees. If the difference in these depths exceeds 1, or if any recursive call indicates the tree is unbalanced, mark the tree as unbalanced.
- Return the depth of the current subtree (1 plus the maximum of the depths of the left and right subtrees).
One key observation is that if any subtree is unbalanced, the entire tree is unbalanced. Thus, if at any step we discover an unbalanced subtree, we can stop our check and return false.
Algorithm Walkthrough:
For the tree:
1
/ \
2 5
/
3
/
4
-
Starting at the root node, which is
1
:- We'll evaluate both the left and right subtrees.
-
For its left child (
2
):- We'll examine both its left and right subtrees.
-
For the left child's left child (
3
):- It has a left child,
4
, with no further descendants. The maximum depth from this node is1
. - It doesn't have a right child, so its depth is
0
. - Difference in depths for node
3
: 1 - 0 = 1 (This is within the allowable range of ≤ 1).
- It has a left child,
-
The left child (
2
) of the root doesn't have a right child. Thus, its depth is0
. -
For the root's left child (
2
):- The maximum depth for its left subtree (from node
3
to4
) is2
. - It doesn't have a right subtree, so its depth is
0
. - Difference in depths for node
2
: 2 - 0 = 2 (This difference is greater than 1, signifying that this subtree is unbalanced).
- The maximum depth for its left subtree (from node
-
For the root's right child (
5
):- It doesn't have a left child or a right child, which means both depths are
0
.
- It doesn't have a left child or a right child, which means both depths are
-
Comparing both children of the root node (
1
):- The left subtree has a maximum depth of
3
. - The right subtree has a depth of
1
. - Difference in depths for node
1
: 3 - 1 = 2 (This exceeds the threshold of 1).
- The left subtree has a maximum depth of
-
Considering the findings from steps 5 and 7, the algorithm determines that the tree is not balanced.
Here is the visual representation of the algorithm:
Code
Here is the code for this algorithm:
Complexity Analysis
Time Complexity: O(n), where n is the number of nodes in the tree. Each node is visited once.
Space Complexity: O(h), where h is the height of the tree. This accounts for the maximum depth of the recursive call stack.
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible