Invert Binary Tree Visualizer & Step-by-Step Algorithm Solution

Invert a binary tree by recursively swapping the left and right child subtrees of every node.

Category: trees | Difficulty: Easy

Tags: DFS, Binary Tree, Recursion, Mirror

Invert Binary Tree

4
2
1
3
7
6
9
100%
Initialization
1/45
Explanation

Start invertTree on binary tree with root: Node(4).

Source Code
1function invertTree(node) {
2 if (!node) return null;
3 const temp = node.left;
4 node.left = node.right;
5 node.right = temp;
6 invertTree(node.left);
7 invertTree(node.right);
8 return node;
9}