Reverse Stored Values on Each Odd Depth of a Perfect Tree

Quick Overview

Reverse the values across every odd depth of a perfect binary tree, preserving the original nodes, links and root identity.

Reverse Stored Values on Each Odd Depth of a Perfect Tree

Company: Josh Technology

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem In the perfect binary tree rooted at `root`, reverse the left-to-right sequence of values separately at each odd depth, where the root has depth 0. Keep all original nodes and child links and return the original root pointer after changing values. ```cpp class Solution { public: TreeNode* reverseOddLevels(TreeNode* root); }; ``` ### Examples **Example 1:** ```text Input: root = [1,2,3,4,5,6,7] Output: [1,3,2,4,5,6,7] Explanation: Depth 1 reverses; depths 0 and 2 keep their values. ``` **Example 2:** ```text Input: root = [9] Output: [9] Explanation: The only level has even depth 0, so no value changes. ``` ### Constraints - The tree contains from 1 through `2^14` nodes. - `0 <= Node.val <= 100000`. - The tree is perfect: each internal node has two children and every leaf has the same depth. ### Clarifying Questions - Is the root at an odd level? No; its depth is 0. - Are values or nodes reversed? Values; keep the existing nodes and child links. - Is reversal limited to sibling pairs? No; reverse the whole left-to-right value sequence at each odd depth. - Which root is returned? The same root pointer after the value changes. ```hint Check the boundary Draw one odd level with more than two nodes and reverse its whole left-to-right sequence. ```

Overview: Reverse the values across every odd depth of a perfect binary tree, preserving the original nodes, links and root identity.

Josh Technology logo
Josh Technology
Source window: 0–6 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

In the perfect binary tree rooted at root, reverse the left-to-right sequence of values separately at each odd depth, where the root has depth 0. Keep all original nodes and child links and return the original root pointer after changing values.

class Solution {
public:
    TreeNode* reverseOddLevels(TreeNode* root);
};

Examples

Example 1:

Input: root = [1,2,3,4,5,6,7]
Output: [1,3,2,4,5,6,7]
Explanation: Depth 1 reverses; depths 0 and 2 keep their values.

Example 2:

Input: root = [9]
Output: [9]
Explanation: The only level has even depth 0, so no value changes.

Constraints

  • The tree contains from 1 through 2^14 nodes.
  • 0 <= Node.val <= 100000 .
  • The tree is perfect: each internal node has two children and every leaf has the same depth.

Clarifying Questions Guidance

  • Is the root at an odd level? No; its depth is 0.
  • Are values or nodes reversed? Values; keep the existing nodes and child links.
  • Is reversal limited to sibling pairs? No; reverse the whole left-to-right value sequence at each odd depth.
  • Which root is returned? The same root pointer after the value changes.