Reverse Odd Levels of Binary Tree - Problem
Transform a Perfect Binary Tree by Reversing Odd Levels
Given the root of a perfect binary tree, your task is to reverse the node values at each odd-numbered level of the tree.
๐ณ What makes this interesting:
โข A perfect binary tree has all parent nodes with exactly two children
โข All leaf nodes are at the same level
โข We need to reverse values within each odd level, not the structure
For example, if level 3 contains values
Level numbering starts at 0:
โข Level 0 (root): Keep as-is
โข Level 1: Reverse values
โข Level 2: Keep as-is
โข Level 3: Reverse values
โข And so on...
Return the root of the modified tree with odd levels reversed.
Given the root of a perfect binary tree, your task is to reverse the node values at each odd-numbered level of the tree.
๐ณ What makes this interesting:
โข A perfect binary tree has all parent nodes with exactly two children
โข All leaf nodes are at the same level
โข We need to reverse values within each odd level, not the structure
For example, if level 3 contains values
[2,1,3,4,7,11,29,18], after reversal it becomes [18,29,11,7,4,3,1,2].Level numbering starts at 0:
โข Level 0 (root): Keep as-is
โข Level 1: Reverse values
โข Level 2: Keep as-is
โข Level 3: Reverse values
โข And so on...
Return the root of the modified tree with odd levels reversed.
Input & Output
example_1.py โ Basic Perfect Binary Tree
$
Input:
root = [2,3,5,8,13,21,34]
โบ
Output:
[2,5,3,8,13,21,34]
๐ก Note:
Level 0: [2] stays the same. Level 1: [3,5] becomes [5,3] (reversed). Level 2: [8,13,21,34] stays the same. The tree structure remains unchanged, only values at odd levels are reversed.
example_2.py โ Larger Perfect Tree
$
Input:
root = [7,13,11]
โบ
Output:
[7,11,13]
๐ก Note:
Level 0: [7] stays the same. Level 1: [13,11] becomes [11,13] (reversed). This is a simple 2-level perfect binary tree where only the children values are swapped.
example_3.py โ Single Node Tree
$
Input:
root = [0]
โบ
Output:
[0]
๐ก Note:
A tree with only one node has no odd levels to reverse (level 0 is even), so it remains unchanged. This is an edge case where the tree is trivially perfect.
Visualization
Tap to expand
Understanding the Visualization
1
Identify Structure
Perfect binary tree ensures complete symmetry at each level
2
Level Classification
Determine which levels are odd (1, 3, 5, ...) and need reversal
3
Symmetric Pairing
Match nodes symmetrically across each odd level
4
Value Swapping
Exchange values between symmetric pairs at odd levels
Key Takeaway
๐ฏ Key Insight: Perfect binary trees have symmetric structure that allows efficient in-place swapping of values at odd levels using DFS traversal
Time & Space Complexity
Time Complexity
O(n)
Visit each node once during DFS traversal
โ Linear Growth
Space Complexity
O(h)
Where h is height of tree due to recursion stack
โ Linear Space
Constraints
- The number of nodes in the tree is in the range [1, 214]
- 0 โค Node.val โค 105
- root is a perfect binary tree
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code