2415. Reverse Odd Levels of Binary Tree #977
-
Topics: Given the
Return the root of the reversed tree. A binary tree is perfect if all parent nodes have two children and all leaves are on the same level. The level of a node is the number of edges along the path between it and the root node. Example 1:
Example 2:
Example 3:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to perform a depth-first traversal on the binary tree. The task is to reverse the node values at odd levels. A perfect binary tree means all non-leaf nodes have two children, and all leaf nodes are at the same level. We will use a DFS (Depth-First Search) approach, and on each odd level, we will reverse the node values. Below is the solution that accomplishes this. Key Points:
Approach:
Plan:
Solution Steps:
Let's implement this solution in PHP: 2415. Reverse Odd Levels of Binary Tree <?php
class TreeNode {
public $val = 0;
public $left = null;
public $right = null;
public function __construct($val = 0, $left = null, $right = null) {
$this->val = $val;
$this->left = $left;
$this->right = $right;
}
}
class Solution {
/**
* @param TreeNode $root
* @return TreeNode
*/
public function reverseOddLevels($root) {
// Call DFS starting from the first level (odd level)
$this->dfs($root->left, $root->right, true);
return $root;
}
/**
* Helper function to perform DFS
*
* @param $left
* @param $right
* @param $isOddLevel
* @return void
*/
private function dfs($left, $right, $isOddLevel) {
// If we reach null nodes, return
if ($left == null) {
return;
}
// If it's an odd level, swap the values
if ($isOddLevel) {
$temp = $left->val;
$left->val = $right->val;
$right->val = $temp;
}
// Recursively move to the next level
$this->dfs($left->left, $right->right, !$isOddLevel);
$this->dfs($left->right, $right->left, !$isOddLevel);
}
}
// Example usage:
$root = new TreeNode(2);
$root->left = new TreeNode(3);
$root->right = new TreeNode(5);
$root->left->left = new TreeNode(8);
$root->left->right = new TreeNode(13);
$root->right->left = new TreeNode(21);
$root->right->right = new TreeNode(34);
$solution = new Solution();
$reversedRoot = $solution->reverseOddLevels($root);
// Function to print the tree for testing
function printTree($root) {
if ($root === null) {
return;
}
echo $root->val . " ";
printTree($root->left);
printTree($root->right);
}
printTree($reversedRoot); // Output: 2 5 3 8 13 21 34
?> Explanation:
Example Walkthrough:Example 1: Input:
Output:
Example 2: Input:
Output:
Time Complexity:
This solution efficiently reverses the nodes at odd levels of a perfect binary tree using depth-first search with a time complexity of O(n). The code swaps values at odd levels and uses a recursive approach to process the tree. |
Beta Was this translation helpful? Give feedback.
We need to perform a depth-first traversal on the binary tree. The task is to reverse the node values at odd levels. A perfect binary tree means all non-leaf nodes have two children, and all leaf nodes are at the same level.
We will use a DFS (Depth-First Search) approach, and on each odd level, we will reverse the node values. Below is the solution that accomplishes this.
Key Points: