Invert Binary Tree - Problem
Given the root of a binary tree, invert the tree and return its root.
To invert a binary tree means to swap the left and right children of every node in the tree recursively.
The original tree structure is preserved, but all left children become right children and vice versa.
Input & Output
Example 1 — Basic Binary Tree
$
Input:
root = [4,2,7,1,3,6,9]
›
Output:
[4,7,2,9,6,3,1]
💡 Note:
The tree is inverted by swapping left and right children at every node. Root 4 stays same, but its children 2 and 7 swap positions, and recursively all subtrees are inverted.
Example 2 — Single Node
$
Input:
root = [2]
›
Output:
[2]
💡 Note:
A single node tree remains unchanged as there are no children to swap.
Example 3 — Empty Tree
$
Input:
root = []
›
Output:
[]
💡 Note:
An empty tree remains empty after inversion.
Constraints
- The number of nodes in the tree is in the range [0, 100]
- -100 ≤ Node.val ≤ 100
Visualization
Tap to expand
Understanding the Visualization
1
Original Tree
Binary tree with values [4,2,7,1,3,6,9]
2
Swap Process
Recursively swap left and right children at each node
3
Inverted Result
Final tree with values [4,7,2,9,6,3,1]
Key Takeaway
🎯 Key Insight: Simply swap left and right children at every node recursively to mirror the tree
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code