PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of binary tree manipulation and traversal methods, emphasizing recursion and iterative approaches as well as the ability to reason about time and space complexity.

  • Medium
  • Palo Alto Networks
  • Coding & Algorithms
  • Software Engineer

Mirror a binary tree and analyze complexity

Company: Palo Alto Networks

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Onsite

Given the root of a binary tree, convert it to its mirror by swapping every node’s left and right children. Implement both recursive and iterative (using a stack or queue) solutions, analyze time and space complexity, and discuss edge cases (empty tree, single node, highly unbalanced). Perform a dry run on this tree (level order): [5,3,8,null,4,7,9].

Quick Answer: This question evaluates understanding of binary tree manipulation and traversal methods, emphasizing recursion and iterative approaches as well as the ability to reason about time and space complexity.

Given a binary tree in level-order list form, convert it into its mirror by swapping every node's left and right children. Your task is to return the mirrored tree in the same compact level-order format, using `None` for missing children when needed and removing trailing `None` values. In an interview, you should also be able to explain both approaches: 1. A recursive DFS solution 2. An iterative solution using a stack or queue Edge cases to think about include an empty tree, a single-node tree, and a highly unbalanced tree. Dry run for `[5, 3, 8, None, 4, 7, 9]`: - Swap children of `5` -> left becomes `8`, right becomes `3` - Swap children of `8` -> left becomes `9`, right becomes `7` - Swap children of `3` -> left becomes `4`, right becomes `None` Final mirrored tree in level order: `[5, 8, 3, 9, 7, 4]`

Constraints

  • 0 <= len(values) <= 100000
  • Each non-null node value is an integer in the range [-10^9, 10^9]
  • The input list represents a valid binary tree in compact level-order form

Examples

Input: ([5, 3, 8, None, 4, 7, 9],)

Expected Output: [5, 8, 3, 9, 7, 4]

Explanation: After mirroring, 8 moves to the left of 5, 3 moves to the right, and their children are swapped as well.

Input: ([],)

Expected Output: []

Explanation: An empty tree remains empty.

Input: ([1],)

Expected Output: [1]

Explanation: A single-node tree is unchanged after mirroring.

Input: ([1, 2, None, 3, None, 4],)

Expected Output: [1, None, 2, None, 3, None, 4]

Explanation: A left-skewed tree becomes a right-skewed tree.

Input: ([2, 2, 2, None, 3, None, 3],)

Expected Output: [2, 2, 2, 3, None, 3]

Explanation: Duplicate values do not change the logic; all left and right pointers are still swapped.

Solution

def solution(values):
    from collections import deque

    class TreeNode:
        __slots__ = ('val', 'left', 'right')

        def __init__(self, val):
            self.val = val
            self.left = None
            self.right = None

    def build_tree(levels):
        if not levels or levels[0] is None:
            return None

        root = TreeNode(levels[0])
        queue = deque([root])
        i = 1

        while queue and i < len(levels):
            node = queue.popleft()

            if i < len(levels):
                left_val = levels[i]
                i += 1
                if left_val is not None:
                    node.left = TreeNode(left_val)
                    queue.append(node.left)

            if i < len(levels):
                right_val = levels[i]
                i += 1
                if right_val is not None:
                    node.right = TreeNode(right_val)
                    queue.append(node.right)

        return root

    def mirror_recursive(node):
        if node is None:
            return None
        node.left, node.right = mirror_recursive(node.right), mirror_recursive(node.left)
        return node

    def mirror_iterative(root):
        if root is None:
            return None

        queue = deque([root])
        while queue:
            node = queue.popleft()
            node.left, node.right = node.right, node.left

            if node.left is not None:
                queue.append(node.left)
            if node.right is not None:
                queue.append(node.right)

        return root

    def serialize(root):
        if root is None:
            return []

        result = []
        queue = deque([root])

        while queue:
            node = queue.popleft()
            if node is None:
                result.append(None)
            else:
                result.append(node.val)
                queue.append(node.left)
                queue.append(node.right)

        while result and result[-1] is None:
            result.pop()

        return result

    root = build_tree(values)

    # Both recursive and iterative mirroring methods are implemented above.
    # The iterative version is used by default to avoid recursion-depth issues
    # on highly unbalanced trees.
    mirrored_root = mirror_iterative(root)

    return serialize(mirrored_root)

Time complexity: O(n) for both recursive and iterative mirroring, where n is the number of nodes.. Space complexity: O(n) overall in this reference solution due to tree construction and serialization; the mirror step itself uses O(h) recursion stack for the recursive version or O(w) queue space for the iterative version, where h is the height and w is the maximum width of the tree..

Hints

  1. At each node, the work is local: swap its left and right children, then continue on the two subtrees.
  2. If the tree can be very deep or highly unbalanced, an iterative traversal with a queue can avoid recursion-depth issues.
Last updated: May 3, 2026

Related Coding Questions

  • Design an O(1) eviction cache - Palo Alto Networks (Medium)
  • Find normalized list difference efficiently - Palo Alto Networks (Medium)

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.