easyTreesGoogleAmazonApple
Locale Layout Inverter
A UI rendering engine flips directory trees and navigation hierarchies to support Right-to-Left (RTL) locales.
Given the root of a binary tree, invert the tree (swap left and right children recursively) and return its root.
Choose approach
4
curr
2
7
1
3
6
9
1/15
Narrationstep 1 of 15
Visit node 4. Swap its left and right descendants.
Variables
curr=4
How to think about it
- DFS traversal guarantees every node is visited.
- At each node, swap left and right subtrees.
- Call recursion on the new left and right subtrees.
Edge cases to catch in interview
- Empty tree -> do nothing.
- Single node -> already inverted.
Examples
Example 1
inverted tree{"arr":[4,2,7,1,3,6,9]}
Example 2
[]{"arr":[]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed