Limited time: AI code review, hints, mock interviews, whiteboard analysis, and all Pro features are unlocked. Enroll

Invert Binary Tree

Easy

Topics
TreeDFSBFS

Given the root of a binary tree, invert it (swap every left and right child) and return the root. Output is the inverted tree in level order.

Example 1

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

Example 2

Input:  root = []
Output: []

Constraints

  • 0 <= number of nodes <= 100
  • -100 <= Node.val <= 100
Run ⌘' · Submit ⌘⏎
SensAI SensAI
Beta
Listening...
Tap mic to stop voice mode

Unlock AI Features

One click to activate - no payment, no credit card. Just sign in and you're in.

AI code review and hints
SensAI chat assistant
AI mock interviews
Whiteboard analysis
100% free during early access