Easy
Oct 06, 2025#binary-tree
226. Invert Binary Tree
Problem
Given the root of a binary tree, invert the tree, and return its root.
Example 1:
Example 2:
Example 3:
Constraints:
- The number of nodes in the tree is in the range [0, 100].
- -100 <= Node.val <= 100
Approach
To solve this problem, we can use a recursive approach.
Our base case will be when the node has neither left or right children or the node itself is null, return here.
Otherwise, we set the left side to the right side and the right side to left side and then call the function on both left and right
Complexity
Time: O(n)
Iterate through the whole tree
Space: O(n)
For the recursive calls.