TIC
The Interns Company

Invert Binary Tree

EasyAcceptance: 72.5%

Problem Statement

Given the root of a binary tree, invert the tree, and return its root. To invert a binary tree, you need to swap the left and right children for each node in the tree.

Constraints:

  • The number of nodes in the tree is in the range [0, 100]
  • -100 <= Node.val <= 100

Input Format:

  • The root of a binary tree

Output Format:

  • The root of the inverted binary tree

Examples:

Example 1:

Input:

root = [4,2,7,1,3,6,9]

Output:

[4,7,2,9,6,3,1]

Explanation:

The original tree is: 4 / \ 2 7 / \ / \ 1 3 6 9 The inverted tree is: 4 / \ 7 2 / \ / \ 9 6 3 1

Example 2:

Input:

root = [2,1,3]

Output:

[2,3,1]

Explanation:

The original tree is: 2 / \ 1 3 The inverted tree is: 2 / \ 3 1

Example 3:

Input:

root = []

Output:

[]

Explanation:

An empty tree remains empty when inverted.

Solutions

Recursive DFS Approach

Time: O(n) where n is the number of nodes in the tree
Space: O(h) where h is the height of the tree (due to recursion stack)

Use a depth-first recursive approach to invert the tree. At each node, recursively invert the left and right subtrees, then swap them.

Iterative BFS Approach

Time: O(n) where n is the number of nodes in the tree
Space: O(n) in the worst case (when the last level has n/2 nodes)

Use a breadth-first search approach with a queue to invert the tree level by level.

Algorithm Walkthrough

Example input: nums = []

Step-by-step execution:

  1. Call invertTree(root) with root node 4
  2. Recursive call to invertTree(root.left) for node 2
  3. Recursive call to invertTree(root.left.left) for node 1
  4. Node 1 has no children, return node 1
  5. Recursive call to invertTree(root.left.right) for node 3
  6. Node 3 has no children, return node 3
  7. Swap children of node 2: left becomes 3, right becomes 1
  8. Return node 2 with swapped children
  9. Recursive call to invertTree(root.right) for node 7
  10. Recursive call to invertTree(root.right.left) for node 6
  11. Node 6 has no children, return node 6
  12. Recursive call to invertTree(root.right.right) for node 9
  13. Node 9 has no children, return node 9
  14. Swap children of node 7: left becomes 9, right becomes 6
  15. Return node 7 with swapped children
  16. Swap children of root node 4: left becomes 7 (with its swapped children), right becomes 2 (with its swapped children)
  17. Return the root node 4 with completely inverted tree

Hints

Hint 1
Think about a recursive approach where you invert the left and right subtrees, then swap them.
Hint 2
You can also use an iterative approach with a queue (BFS) or stack (DFS).
Hint 3
Remember to handle the empty tree case.

Video Tutorial

Video tutorials can be a great way to understand algorithms visually

Visualization

Visualize the recursive process of swapping left and right children at each node in the tree.

Key visualization elements:

  • current node
  • left subtree
  • right subtree
  • swapped children

Implementation Notes

This problem gained notoriety when Max Howell (creator of Homebrew) tweeted that Google rejected him for not being able to invert a binary tree, despite millions of people using his software. It's now considered a classic binary tree problem and a good introduction to tree recursion.