Reverse Odd Levels of Binary Tree (LeetCode 2415): Solution and Explanation

Medium
Tree
Depth First Search
Breadth First Search
Binary Tree

In a perfect binary tree, reverse the values on every odd level (1, 3, 5, ...) and leave the even levels as they are. Only values change; the tree's shape stays the same.

Intuition

Reversing a row of values means the first and last trade places, then the second and second to last, and so on toward the middle.

In a perfect tree those partners are mirror images of each other across the vertical line through the root. The root's left child mirrors its right child, the left child's left child mirrors the right child's right child, and so on.

So one DFS can walk the tree two nodes at a time, always holding a mirror pair. On an odd level it swaps the pair's values. Every pair on that level gets swapped exactly once, which reverses the row.

Approach: Mirror-pair DFS

  1. Define dfs(a, b, level), where a and b are mirror images on the same level.
  2. If a is null, stop. In a perfect tree b is null at the same time.
  3. If level is odd, swap a.val and b.val.
  4. Recurse on the outer pair (a.left, b.right) and the inner pair (a.right, b.left) with level + 1.
  5. Call dfs(root.left, root.right, 1) and return root.

Step-by-step example

root = [2,3,5,8,13,21,34]
  1. Start with the pair (3, 5) on level 1. The level is odd, so swap: the left child now holds 5 and the right child holds 3.
  2. Recurse on the outer pair (8, 34) on level 2. The level is even, so nothing changes.
  3. Recurse on the inner pair (13, 21) on level 2. Even again, so nothing changes.
  4. The leaves have no children, so the DFS ends. Return [2,5,3,8,13,21,34].

Code: Python, Java, C++

1class Solution:
2    def reverseOddLevels(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
3        def dfs(a, b, level):
4            if not a:
5                return
6            if level % 2 == 1:
7                a.val, b.val = b.val, a.val
8            dfs(a.left, b.right, level + 1)
9            dfs(a.right, b.left, level + 1)
10
11        dfs(root.left, root.right, 1)
12        return root
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each non-root node belongs to exactly one mirror pair, and each pair is visited once.

Space

O(log n)

The recursion depth equals the tree height, which is log n for a perfect tree.

Other approaches

BFS with a level list (time O(n), space O(n))

Process the tree level by level. On odd levels, use two pointers from both ends of the level list and swap values as they move inward.

Rebuild from level order (time O(n), space O(n))

Read the values into level lists, reverse the odd ones, and write them back in the same order. Simple but uses extra memory for every value.

Common mistakes

  • Swapping the children of a node instead of values across the whole level. That only reverses pairs of siblings, not the full row.
  • Pairing a.left with b.left. Mirror pairs are outer with outer (a.left, b.right) and inner with inner (a.right, b.left).
  • Counting the root as level 1. The root is level 0, so its children form the first odd level.

Frequently asked questions

Why is it enough to swap values instead of moving nodes?

The output only needs the right value at each position. Swapping values keeps the structure fixed and avoids relinking pointers.

Does this need the tree to be perfect?

The mirror pairing relies on every level being full, so each node has a partner. The problem guarantees a perfect tree.

Why recurse into even levels if nothing changes there?

The next level down is odd, and the only way to reach those pairs is through their parents on the even level.