Skip to main content
All Questions

Invert binary tree.

Easy
Unlock detailed company stats for this questionUpgrade

Given the root of a binary tree, invert it by swapping the left and right children of every node, and return its root.

Trees in the examples and tests are written in level order, with null for a missing child.

Example 1

Input: root = [4, 2, 7, 1, 3, 6, 9] Output: [4, 7, 2, 9, 6, 3, 1] 4 4 / \ / \ 2 7 -> 7 2 / \ / \ / \ / \ 1 3 6 9 9 6 3 1

Example 2

Input: root = [2, 1, 3] Output: [2, 3, 1]

Constraints

  • The tree has between 0 and 100 nodes.
  • -100 <= Node.val <= 100

Related courses

Course

Data Analyst Interview Prep

Ace your data analyst interviews—whether you're targeting product, marketing, or business analyst roles. Tackle real take-home case studies, sharpen your technical and dashboarding skills, and get strategies from interviewers at top tech companies and startups.

Course

SQL Interview Prep Course

Prepare for SQL interviews with lessons on joins, aggregations, and window functions, then practice real SQL interview questions in an interactive editor with expert solutions.