Trees & Tree DFS/BFS/Easy

Invert Binary TreePRO PASS

Time: O(N)Space: 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 / 333%
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
}

Custom Test Case Runner

Input your custom values and visualize step-by-step trace

Quick Presets:

AI DSA Coach

Contextual Tutor for Invert Binary Tree

Hello! I am your AI DSA Tutor for **Invert Binary Tree** (Trees & Tree DFS/BFS). Ask me anything about this algorithm, time complexity, or request a step hint!