Back

Invert Binary Tree

easy

Given the root of a binary tree, invert the tree (mirror it left-to-right), and return its root.

Test Cases

Copy an input into the main harness and Run to verify
Input
root = [4,2,7,1,3,6,9]
Expected Output
[4,7,2,9,6,3,1]
Input
root = [2,1,3]
Expected Output
[2,3,1]
Input
root = []
Expected Output
[]

Constraints

  • The number of nodes in the tree is in the range [0, 100].
  • -100 <= Node.val <= 100

Hints

Hint 1 — click to reveal

Swap every node's children.

Hint 2 — click to reveal

Recurse into both subtrees after swapping.

Java Compiler

Powered by OneCompiler. Starter code loads automatically — edit and hit Run.