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

Approaches

Related problems

Start free on Cariara · Coding problems · System design studio

Starting Python
No results yet.

          
↑↓ move ↵ open esc close

Labs

Loading labs…

Run with your own input

Starting Python

Loading Python…