Invert Binary Tree
Given the root of a binary tree represented as an array in level order, invert the tree (mirror left and right subtrees) and return its level-order representation. **Examples:** ```typescript invertTree([4, 2, 7, 1, 3, 6, 9]) // [4, 7, 2, 9, 6, 3, 1] invertTree([2, 1, 3]) // [2, 3, 1] invertTree([]) // [] ```
Solve It in the BrowserPractice Explaining It Out Loud
Optimal Approach
- Time complexity: O(n)
- Space complexity: O(n)
Every node in the tree is visited once to swap its left and right child pointers.
Follow-Up Questions Interviewers Ask About Invert Binary Tree
- Can you invert a tree iteratively instead of recursively?
Yes, using BFS with a queue or DFS with a stack to visit each node and swap its left and right pointers.
Practice the full problem with execution, tests, and AI code review in the interactive lab — then rehearse explaining your solution in a live voice mock interview.