Binary Tree Upside Down - Problem

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

example_1.py โ€” Basic Tree
$ Input: root = [1,2,3,4,5]
โ€บ Output: [4,5,2,null,null,3,1]
๐Ÿ’ก Note: The original tree has root 1 with children 2,3. Node 2 has children 4,5. After flipping upside down, node 4 (leftmost leaf) becomes the new root. Node 2 becomes 4's right child, and 5 becomes 4's left child. This pattern continues up the tree.
example_2.py โ€” Single Node
$ Input: root = [1]
โ€บ Output: [1]
๐Ÿ’ก Note: A single node tree remains unchanged when flipped upside down since there are no children to rearrange.
example_3.py โ€” Empty Tree
$ Input: root = []
โ€บ Output: []
๐Ÿ’ก Note: An empty tree (null root) remains empty after the upside-down transformation.

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

Visualization

Tap to expand
Original Tree12345Flip Upside DownLeft child becomes new rootUpside Down Tree45231Transformation RulesPOriginal ParentRBecomes Right ChildLOriginal Left ChildLBecomes New RootROriginal Right ChildRBecomes Left Child
Understanding the Visualization
1
Identify the Pattern
The leftmost leaf becomes the new root of the entire tree
2
Process Bottom-Up
Start from the deepest level and work your way up, transforming relationships
3
Apply Transformation Rules
For each node: left child โ†’ new parent, original node โ†’ right child, right sibling โ†’ left child
4
Return New Root
The transformation cascades up, returning the new root of the upside-down tree
Key Takeaway
๐ŸŽฏ Key Insight: The upside-down transformation follows a consistent pattern where each left child becomes the new parent, original parents become right children, and right siblings become left children. Process recursively from the leftmost path!
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28 Apple 22
78.2K Views
Medium Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen