Class Solution
java.lang.Object
g2401_2500.s2415_reverse_odd_levels_of_binary_tree.Solution
2415 - Reverse Odd Levels of Binary Tree.<p>Medium</p>
<p>Given the <code>root</code> of a <strong>perfect</strong> binary tree, reverse the node values at each <strong>odd</strong> level of the tree.</p>
<ul>
<li>For example, suppose the node values at level 3 are <code>[2,1,3,4,7,11,29,18]</code>, then it should become <code>[18,29,11,7,4,3,1,2]</code>.</li>
</ul>
<p>Return <em>the root of the reversed tree</em>.</p>
<p>A binary tree is <strong>perfect</strong> if all parent nodes have two children and all leaves are on the same level.</p>
<p>The <strong>level</strong> of a node is the number of edges along the path between it and the root node.</p>
<p><strong>Example 1:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2022/07/28/first_case1.png" alt="" /></p>
<p><strong>Input:</strong> root = [2,3,5,8,13,21,34]</p>
<p><strong>Output:</strong> [2,5,3,8,13,21,34]</p>
<p><strong>Explanation:</strong></p>
<p>The tree has only one odd level.</p>
<p>The nodes at level 1 are 3, 5 respectively, which are reversed and become 5, 3.</p>
<p><strong>Example 2:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2022/07/28/second_case3.png" alt="" /></p>
<p><strong>Input:</strong> root = [7,13,11]</p>
<p><strong>Output:</strong> [7,11,13]</p>
<p><strong>Explanation:</strong></p>
<p>The nodes at level 1 are 13, 11, which are reversed and become 11, 13.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> root = [0,1,2,0,0,0,0,1,1,1,1,2,2,2,2]</p>
<p><strong>Output:</strong> [0,2,1,0,0,0,0,2,2,2,2,1,1,1,1]</p>
<p><strong>Explanation:</strong></p>
<p>The odd levels have non-zero values.</p>
<p>The nodes at level 1 were 1, 2, and are 2, 1 after the reversal.</p>
<p>The nodes at level 3 were 1, 1, 1, 1, 2, 2, 2, 2, and are 2, 2, 2, 2, 1, 1, 1, 1 after the reversal.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 2<sup>14</sup>]</code>.</li>
<li><code>0 <= Node.val <= 10<sup>5</sup></code></li>
<li><code>root</code> is a <strong>perfect</strong> binary tree.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
reverseOddLevels
-