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 [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
Perfect Binary Tree: Reverse Odd LevelsBefore:2Level 0 (Even)35Level 1 (Odd)8132134Level 2 (Even)After:2Level 0 (Even)53Level 1 (Reversed!)8132134Level 2 (Even)REVERSEAlgorithm Steps:1. Even levels (0,2,4,...) remain unchanged2. Odd levels (1,3,5,...) have their values reversed left-to-right3. Tree structure stays the same, only node values are modified
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

n
2n
โœ“ Linear Growth
Space Complexity
O(h)

Where h is height of tree due to recursion stack

n
2n
โœ“ 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
Asked in
Microsoft 25 Amazon 18 Google 15 Meta 12
42.3K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen