Similar Problems

Similar Problems not available

Reverse Odd Levels Of Binary Tree - Leetcode Solution

Companies:

LeetCode:  Reverse Odd Levels Of Binary Tree Leetcode Solution

Difficulty: Medium

Topics: binary-tree tree depth-first-search breadth-first-search  

Problem Statement:

Given the root of a binary tree, reverse the tree on alternate levels, starting from level 1. Level 1 is the root of the tree.

Example:

Input:

      1
   /     \
 2         3

/ \ /
4 5 6 7

Output:

      1
   /     \
 3         2

/ \ /
4 5 6 7

Explanation: The odd levels of the tree (level 1 and 3) are reversed. Therefore, the output is the original tree with the nodes at level 1 and 3 swapped.

Solution:

We can solve this problem using a recursive approach. We can pass a level number to the helper function to determine if the level is odd or even. If the level is odd, we reverse the nodes at that level.

Here is the algorithm to solve this problem:

  1. Create a helper function that takes the root of the binary tree, level number, and a boolean flag to determine if the level is odd or even.

  2. If the root is null, return.

  3. If the level is odd, reverse the nodes at that level using the two-pointer approach. We start from the leftmost node and the rightmost node of that level and swap them. Then, we move the left pointer to the right and the right pointer to the left until they meet in the middle.

  4. Recursively call the helper function on the left and right child of the root with the level incremented by 1 and the boolean flag inverted.

  5. Return the root.

Here is the code implementation of this algorithm:

void reverseNodes(Node *left, Node *right) {
    while (left && right) {
        swap(left->val,right->val);
        left = left->next;
        right = right->prev;
    }
}

Node* reverseOddLevels(Node *root) {
    if (!root) return NULL;
    
    bool isOdd = true;
    Node *left = root, *right = NULL;
    while (left) {
        Node *prev = NULL, *curr = left;
        left = NULL;
        
        while (curr) {
            if (isOdd) {
                if (curr->left) {
                    if (!left) left = curr->left;
                    right = curr->left;
                }
                else if (curr->right) {
                    if (!left) left = curr->right;
                    right = curr->right;
                }
                
                if (left && right) {
                    reverseNodes(left,right);
                    left = NULL;
                    right = NULL;
                }
            }
            else {
                left = (left ? left : curr->left ? curr->left : curr->right);
                right = (right ? right : curr->right ? curr->right : curr->left);
            }
            
            if (prev) prev->next = curr->left;
            curr->left->prev = prev;
            prev = curr->left;
            
            if (prev) prev->next = curr->right;
            curr->right->prev = prev;
            prev = curr->right;
            
            curr = curr->next;
        }
        
        isOdd = !isOdd;
    }
    
    return root;
}

The time complexity of this algorithm is O(n), where n is the number of nodes in the binary tree, as we visit each node once. The space complexity is O(h), where h is the height of the binary tree, due to the recursive calls.

Reverse Odd Levels Of Binary Tree Solution Code

1