Back to DSA

Invert Binary Tree

easy
Acceptance: 68%
Binary Tree

Given the root of a binary tree, produce its mirror image by swapping the left and right children at every node throughout the tree. Return the root of the transformed tree.

Examples

Example 1:
Input:root = [1,2,3,4,5,6,7]
Output:[1,3,2,7,6,5,4]
Example 2:
Input:root = [8,6,10]
Output:[8,10,6]

Hints

00:00
class Solution {
    public TreeNode invertTree(TreeNode root) {
        if (root == null) return null;
        TreeNode temp = root.left;
        root.left = invertTree(root.right);
        root.right = invertTree(temp);
        return root;
    }
}
Time complexityO(n)
Space complexityO(h)