Problem
You get the root root of a binary tree. In a binary tree, each node has at most two children: a left child and a right child.
Invert the tree. To invert a tree, swap the left child and the right child of every node. Return the root of the inverted tree.
The examples show each tree as a list in level order. This list gives the nodes row by row, from left to right. The value null marks a missing child.
Example 1
- Input:
- root = [5,3,8,1,4,7,9]
- Output:
- [5,8,3,9,7,4,1]
- Explanation:
- Each row of the inverted tree has the values of the same input row in reverse order. For example, the row
1, 4, 7, 9becomes9, 7, 4, 1.
Example 2
- Input:
- root = [10,4,null,2]
- Output:
- [10,null,4,null,2]
- Explanation:
- The input tree has only left children. The inverted tree has only right children.
Example 3
- Input:
- root = []
- Output:
- []
- Explanation:
- An empty tree stays empty.
Constraints
- 0 <= number of nodes <= 100
- -100 <= Node.val <= 100
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Invert binary tree and matrix class implementation
Coding
- All Nodes Distance K in Binary Tree
Coding · Binary Tree · Breadth-First Search · Depth-First Search
- Amount of Time for Binary Tree to Be Infected
Coding · Binary Tree · Breadth-First Search · Depth-First Search
- Average of Levels in Binary Tree
Coding
Sources · 1 reference
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 1 record
Target
Leetcode 226. Invert Binary Tree
Reported Jul 20, 2026 · 1 source report
SENIOR
Source snapshot: Nov 12, 2026
View source