Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Invert Binary Tree

Easy
treebinary-treedfsbfs

Invert a binary tree (mirror it left-to-right) and return its root.

Input: one line — the tree in level order, null for missing children. Output: the inverted tree in level order (trailing nulls trimmed; null for an empty tree).

Example 1
Input:
4 2 7 1 3 6 9
Output:
4 7 2 9 6 3 1
Example 2
Input:
2 1 3
Output:
2 3 1
Example 3
Input:
null
Output:
null

Constraints

  • 0 ≤ number of nodes ≤ 100
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.