Dsa
Trees
Easy
Invert Binary Tree
Write a function to invert a binary tree. Inverting a binary tree means swapping the left and right child of all nodes in the tree. The input will be given in level order.
Input format:
- The first line contains an integer N, the number of nodes in the binary tree.
- The next N lines describe the nodes in level order where -1 indicates a null node.
Output format:
- Print the inverted tree nodes in level order with space-separated values. Use -1 for null nodes.
Example:
Input:
5
1
2
3
-1
-1
4
5
Output:
1 3 2 5 4 -1 -1
Key concepts
treestree manipulation
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now