Invert Binary Tree
Easy · Trees · Tree · DFS · BFS · Recursion
Problem
Given the root of a binary tree, swap every node's left and right child, and return the root.
The node type and two helpers are provided:
Examples
Input: root = [4,2,7,1,3,6,9] Output: [4,7,2,9,6,3,1]
Every level is mirrored.
Input: root = [] Output: []
An empty tree inverts to an empty tree.
Constraints
- 0 <= number of nodes <= 100
- -100 <= Node.val <= 100
Approaches
- Iterative with an explicit queue — time O(n), space O(n)
Related problems
Start free on Cariara · Coding problems · System design studio