Given the root of a binary tree, invert the tree, and return its root.
Example 1:
Input: root = [4,2,7,1,3,6,9] Output: [4,7,2,9,6,3,1]
Example 2:
Input: root = [2,1,3] Output: [2,3,1]
Example 3:
Input: root = [] Output: []
Constraints:
[0, 100].-100 <= Node.val <= 100Problem Overview: You’re given the root of a binary tree. The task is to invert the tree by swapping the left and right child of every node. After inversion, nodes that were originally on the left move to the right and vice versa across the entire tree.
Approach 1: Recursive Depth-First Search (O(n) time, O(h) space)
This approach uses recursion with Depth-First Search. For each node, swap its left and right pointers, then recursively process both children. The key insight is that inversion is a local operation—once a node’s children are swapped, the same rule applies independently to its subtrees. Each node is visited exactly once, giving O(n) time complexity where n is the number of nodes. The recursion stack uses O(h) space, where h is the tree height. For balanced trees this is O(log n), but in the worst case (skewed tree) it becomes O(n). This method is concise and maps directly to the recursive structure of a Binary Tree.
The algorithm is straightforward: if the current node is null, return immediately. Otherwise swap its children, then recursively call the same function on the new left and right children. Because the swap happens before recursion, every subtree ends up inverted correctly. This is the most common implementation you’ll see in interviews and editorials.
Approach 2: Iterative BFS Using a Queue (O(n) time, O(w) space)
This version performs a level-order traversal using Breadth-First Search. Start by pushing the root node into a queue. While the queue is not empty, pop the front node, swap its left and right children, and then enqueue the children if they exist. The queue ensures nodes are processed level by level.
Each node is dequeued exactly once and swapped once, so the total runtime is O(n). Space complexity depends on the maximum width of the tree. In the worst case, the queue holds up to O(w) nodes, where w is the maximum number of nodes in any level (up to O(n) for a complete tree).
The BFS approach avoids recursion and stack depth concerns. It’s a good choice when working in environments where recursion depth might cause stack overflow or when you want an explicit iterative traversal.
Recommended for interviews: The recursive DFS solution is typically expected because it directly reflects the structure of the tree and requires minimal code. Interviewers often accept the BFS queue solution as well, especially if you explain the traversal tradeoffs. Showing both approaches demonstrates strong understanding of tree traversal patterns.
This approach employs recursion to invert the binary tree. The basic idea is to traverse the tree and swap the left and right children of every node recursively. We recursively call the function on the left and right subtrees until we reach the base case, which is when the node is null.
This C solution defines a struct for the tree node, and then uses a recursive function invertTree that swaps the left and right children of each node starting from the root. The recursion continues until all nodes have been swapped.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n) where n is the number of nodes in the tree, because each node is visited once.
Space Complexity: O(h) where h is the height of the tree, for the recursion stack.
The iterative approach involves using a queue or stack to traverse the tree level by level (or depth by depth) while inverting the child nodes by putting the left child in place of the right child and vice versa for each node. This is effectively a breadth-first traversal that swaps children iteratively.
The C solution uses a queue data structure to perform a breadth-first traversal. For each node, it swaps the left and right children and then enqueues the children nodes if they exist.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n) where n is the number of nodes in the tree.
Space Complexity: O(n) due to the queue data structure.
| Approach | Complexity |
|---|---|
| Recursive Approach | Time Complexity: O(n) where n is the number of nodes in the tree, because each node is visited once. |
| Iterative Approach Using a Queue | Time Complexity: O(n) where n is the number of nodes in the tree. |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recursive DFS | O(n) | O(h) | Most common solution. Clean and concise for binary tree recursion. |
| Iterative BFS (Queue) | O(n) | O(w) | Useful when avoiding recursion or when using level-order traversal. |
Invert Binary Tree - Depth First Search - Leetcode 226 • NeetCode • 287,306 views views
Watch 9 more video solutions →Practice Invert Binary Tree with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor