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
Binary Tree Upside Down INPUT Original Binary Tree 1 2 3 4 5 root = [1,2,3,4,5] Left child: 2, Right child: 3 Node 2's children: 4, 5 Leftmost path: 1 --> 2 --> 4 ALGORITHM STEPS 1 Traverse Left Path Go to leftmost node (4) 2 Flip at Each Level Left becomes new root 3 Reassign Pointers Parent --> right child Sibling --> left child 4 Clear Old Pointers Set old left/right to null Transformation Rules: old.left --> new root old root --> new.right old.right --> new.left Time: O(n) | Space: O(n) FINAL RESULT Flipped Binary Tree 4 5 2 3 1 [4,5,2,null,null,3,1] OK - Tree Flipped! New root: 4 Key Insight: The transformation follows the leftmost path of the tree. At each node, we flip the parent-child relationship: left child becomes parent, original parent becomes right child, and sibling becomes left child. This can be done recursively or iteratively by following the left spine. TutorialsPoint - Binary Tree Upside Down | Optimal Solution
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