Trees & Tree DFS/BFS · Easy

Invert Binary Tree

O(N) · O(H)

Step 1: Setup & Initialization - Invert Binary Tree

Recursively swap left and right subtrees at each node.

Array Elements & Pointers
10
[0]
20
[1]
30
[2]
40
[3]
50
[4]
Live Variables & Invariants
status:Initialized
pattern:Invert Binary Tree
Step 1 / 3
33%
Solution Code
1
function invertTree(root: TreeNode | null): TreeNode | null {
2
  if (!root) return null;
3
  const temp = root.left;
4
  root.left = invertTree(root.right);
5
  root.right = invertTree(temp);
6
  return root;
7
}
LeetCode IDE Console

Test Cases

3 cases from Blind 75 & NeetCode 150

Ask for a hint whenever you get stuck.