Solution: Invert Binary Tree

Let's solve the Invert Binary Tree problem using the Tree Depth-first Search pattern.

Statement#

Given the root node of a binary tree, transform the tree by swapping each node’s left and right subtrees, thus creating a mirror image of the original tree. Return the root of the transformed tree.

Constraints:

  • 00\leq Number of nodes in the tree 100\leq 100
  • 1000-1000 \leq Node.value 1000\leq 1000

Solution#

For this solution, we do a post-order (left, right, parent) traversal of the binary tree. The algorithm is as follows:

  • Perform post-order traversal on the left child of the root node.
  • Perform post-order traversal on the right child of the root node.
  • Swap the left and right children of the root node.

Since we perform a depth-first search traversal, the children of any node are already mirrored even before we return the node itself.

Let’s look at an example below:

Note: In this example, the node at the top of the stack is the current node.

Created with Fabric.js 3.6.6 20 50 200 75 25 300 top stack Initial setup. The call stack shows the recursive callsmade during the postorder traversal.

1 of 16

Created with Fabric.js 3.6.6 top mirror_binary_tree(20) stack 20 50 200 75 25 300 Start the postorder traversal at the root.

2 of 16

Created with Fabric.js 3.6.6 top mirror_binary_tree(50) mirror_binary_tree(20) stack 20 50 200 75 25 300 Call mirror_binary_tree() on the left subtree.

3 of 16

Created with Fabric.js 3.6.6 top mirror_binary_tree(75) mirror_binary_tree(50) mirror_binary_tree(20) stack 20 50 200 75 25 300 Call mirror_binary_tree() on the left subtree.

4 of 16

Created with Fabric.js 3.6.6 top mirror_binary_tree(50) mirror_binary_tree(20) stack 20 50 200 75 25 300 Node 75 does not have a left or right child,hence, it returns.

5 of 16

Created with Fabric.js 3.6.6 top mirror_binary_tree(25) mirror_binary_tree(50) mirror_binary_tree(20) stack 20 50 200 75 25 300 Call mirror_binary_tree on the right child of node with value 50.

6 of 16

Created with Fabric.js 3.6.6 20 50 200 75 25 300 top mirror_binary_tree(50) mirror_binary_tree(20) stack Thr right subtree of node 50 also returns.This indicates that both subtrees havealready been mirrored.

7 of 16

Created with Fabric.js 3.6.6 top mirror_binary_tree(50) mirror_binary_tree(20) stack 20 50 200 25 75 300 Mirror (swap) the children of node 50.curr.left, curr.right = curr.right, curr.left

8 of 16

Created with Fabric.js 3.6.6 top mirror_binary_tree(20) stack 20 50 200 25 75 300 Node 50 also returns.

9 of 16

Created with Fabric.js 3.6.6 top mirror_binary_tree(200) mirror_binary_tree(20) stack 20 50 200 25 75 300 Call mirror_binary_tree() on the right subtree.

10 of 16

Created with Fabric.js 3.6.6 top mirror_binary_tree(300) mirror_binary_tree(200) mirror_binary_tree(20) stack 20 50 200 25 75 300 Call mirror_binary_tree() on the right subtree.

11 of 16

Created with Fabric.js 3.6.6 20 50 200 25 75 300 top mirror_binary_tree(200) mirror_binary_tree(20) stack Node 300 returns.

12 of 16

Created with Fabric.js 3.6.6 top mirror_binary_tree(200) mirror_binary_tree(20) stack Mirror (swap) theleft (NULL) and right (300) nodes of 200.curr.left, curr.right = curr.right, curr.left 20 50 200 25 75 300

13 of 16

Created with Fabric.js 3.6.6 top mirror_binary_tree(200) mirror_binary_tree(20) stack Node 200 returns. 20 50 200 25 75 300

14 of 16

Created with Fabric.js 3.6.6 top mirror_binary_tree(20) stack 20 200 50 300 75 25 Mirror (swap) theleft (50) and right (200) nodes of 20.curr.left, curr.right = curr.right, curr.left

15 of 16

Created with Fabric.js 3.6.6 top stack 20 200 50 300 75 25 Node 20 returns and weare done mirroring the binary tree.

16 of 16

Let’s look at the code for this solution below:

main.py
BinaryTree.py
TreeNode.py
Invert Binary Tree

Time complexity#

The time complexity of this solution is linear, O(n)O(n), where nn is the number of nodes in the tree.

Note: Every subtree needs to be mirrored, so we visit every node once. Because of that, the runtime complexity is O(n)O(n).

Space complexity#

The space complexity of this solution is O(h)O(h), where hh is the height of the tree. This is because our recursive algorithm uses space on the call stack, which can grow to the height of the binary tree. The complexity will be O(logn)O(\log n) for a balanced tree and O(n)O(n) for a degenerate tree.

Invert Binary Tree

Maximum Depth of Binary Tree