By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
"This problem is a litmus test for recursion and tree traversal—Google famously asked it in their interviews, and nailing it proves you can handle hierarchical data structures with clarity and efficiency."
Before tackling this problem, ensure you understand: 1. Binary Tree Basics – Structure, nodes, left/right children, root. 2. Tree Traversal (DFS/BFS) – Depth-First Search (pre-order, in-order, post-order) and Breadth-First Search (level-order). 3. Recursion vs. Iteration – How to implement both for tree problems.
root == null
null
Follow this repeatable mental checklist for every binary tree inversion problem:
Ask: "Can the tree be empty? Can it have only one child?"
Choose a Traversal Strategy
Iterative DFS: Best for deep trees where recursion might stack overflow.
Base Case Handling
If root.left == null && root.right == null, return root (no inversion needed).
root.left == null && root.right == null
root
Swap Children
left
right
Example: root.left, root.right = root.right, root.left
root.left, root.right = root.right, root.left
Recurse or Iterate
Iterative: Use a queue/stack to process nodes level by level.
Edge Case Testing
Test with:
Time & Space Complexity
Problem: Invert a binary tree recursively.
Thought Process: 1. Base case: If root is null, return null. 2. Swap left and right children. 3. Recursively invert left and right subtrees.
Python Code:
class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def invertTree(root): if not root: return None # Swap left and right children root.left, root.right = root.right, root.left # Recursively invert subtrees invertTree(root.left) invertTree(root.right) return root
Time Complexity: O(N) (visit every node once). Space Complexity: O(H) (recursion stack, where H is tree height).
Why This Works: - Post-order traversal ensures children are processed before the parent, so swapping happens from the bottom up.
Problem: Invert a binary tree iteratively using BFS.
Thought Process: 1. Use a queue to process nodes level by level. 2. For each node, swap its left and right children. 3. Enqueue the children for further processing.
from collections import deque def invertTree(root): if not root: return None queue = deque([root]) while queue: node = queue.popleft() # Swap left and right children node.left, node.right = node.right, node.left # Enqueue children if they exist if node.left: queue.append(node.left) if node.right: queue.append(node.right) return root
Time Complexity: O(N) (visit every node once). Space Complexity: O(N) (queue storage, worst-case for a full tree).
Why This Works: - BFS processes nodes level by level, ensuring all children are swapped before moving to the next level.
Problem: Invert a binary tree iteratively using DFS (post-order simulation).
Thought Process: 1. Use a stack to simulate post-order traversal. 2. Push nodes onto the stack, then process them in reverse order (children before parent). 3. Swap children when popping from the stack.
def invertTree(root): if not root: return None stack = [root] while stack: node = stack.pop() # Swap left and right children node.left, node.right = node.right, node.left # Push children onto the stack (right first, then left) if node.right: stack.append(node.right) if node.left: stack.append(node.left) return root
Time Complexity: O(N) (visit every node once). Space Complexity: O(N) (stack storage, worst-case for skewed tree).
Why This Works: - The stack ensures we process nodes in a way that mimics post-order traversal, swapping children before their parent.
NullPointerException
if not root
"Alright, let’s lock this in. Inverting a binary tree is all about traversal and swapping. Here’s the playbook:
Time complexity is always O(N), space depends on your method. Recursive is O(H), iterative is O(N).
Now go crush that interview—you’ve got this!
This guide is 100% interview-ready—every line is actionable under pressure. Good luck! ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.