Spot the unbalanced node
Warm-up · Trace the algorithm · about 5 min
The keys 1, 2, 3, 4, 5 are inserted in this order into an empty AVL tree. Each node shows its balance factor: left height minus right height.
Answer each question before the step plays.
Example 1
Input:
[[1,2,3,4,5]]
Output:
[2,1,4,null,null,3,5]
The final tree in level order.
Constraints
- A node is out of balance when its balance factor is below -1 or above +1
- The lowest unbalanced node on the path is repaired first
Learn the idea first: AVL trees and rotations, a visual lesson.
More AVL trees and rotations problems
- Identify the rotation Warm-up
- Insert and rebalance Standard
- Implement rotate-left Standard
- Implement rotate-right Standard
- Implement AVL insert Challenge