Imagine you have a binary tree that needs to be completely flipped upside down! This isn't just a simple rotation - it's a systematic transformation where each level undergoes a specific rearrangement.
Given the root of a binary tree, you need to turn the entire tree upside down and return the new root. The transformation follows these rules for each node:
- The original left child becomes the new root
- The original root becomes the new right child
- The original right child becomes the new left child
This process is applied level by level throughout the tree. The problem guarantees that every right node has a corresponding left sibling (same parent) and that right nodes have no children, making this transformation always possible.
Example: If you have a tree like 1-2-3, after flipping it becomes a completely different structure where the leftmost bottom node becomes the new root!
Input & Output
Constraints
- The number of nodes in the tree is in the range [0, 1000]
- -1000 โค Node.val โค 1000
- Every right node has a sibling (left node with same parent) and no children
- The tree structure guarantees a valid upside-down transformation is possible