Dashboard
#25. Invert Binary TreeEasyTrees

You are given the root of a binary tree root. Invert the binary tree and return its root.

Example 1:

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

Output: [1,3,2,7,6,5,4]

Example 2:

Input: root = [3,2,1]

Output: [3,1,2]

Example 3:

Input: root = []

Output: []

Constraints:

  • 0 <= The number of nodes in the tree <= 100.
  • -100 <= Node.val <= 100
Examples
Example 1:
Input: root = [1,2,3,4,5,6,7]
Output: [1,3,2,7,6,5,4]
Example 2:
Input: root = [3,2,1]
Output: [3,1,2]
Example 3:
Input: root = []
Output: []
Constraints
  • `0 <= The number of nodes in the tree <= 100`.
  • `-100 <= Node.val <= 100`
Company Frequency
Bloomberg6Google3Meta3LinkedIn3Microsoft2Amazon2Oracle2Palo Alto Networks2
Prerequisites
Target: O(N) time • O(1) spaceTree / Depth-First Search / Breadth-First Search / Binary Tree
Socratic AI Tutor
Gemini 3.8 Flash
Loading editor...

Click "Run" or press Ctrl+Enter to execute test cases.