ExamAdda Logo

Invert Binary Tree

Easy

Description

Given the root of a binary tree, invert the tree and return its root.

To invert a binary tree, swap the left and right children of every node.

For example:

     4  

    / \   

  2   7   

 / \ / \  

1  3 6  9

 

becomes:

     4   

    / \   

   7   2   

  / \ / \  

 9  6 3  1

Example 1

Input

root = [4,2,7,1,3,6,9]

Output

[4,7,2,9,6,3,1]

Explanation

The left and right children of every node are swapped.

Example 2

Input

root = [2,1,3]

Output

[2,3,1]

Explanation

The left child 1 and right child 3 of the root are swapped.

Constraints

0 <= number of nodes <= 100
-100 <= Node.val <= 100

Hints:

Hint 1

For every node, swap its left and right children.

Hint 2

After swapping the children of the current node, recursively invert both subtrees.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 11, 2026

Last updated

Aug 12, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output