Change the Root of a Binary Tree - Problem
Change the Root of a Binary Tree is a fascinating tree transformation problem that challenges you to restructure an entire binary tree by making any leaf node the new root.

Given the root of a binary tree and a specific leaf node, you need to reroot the tree so that the leaf becomes the new root. This involves reversing the parent-child relationships along the path from the leaf to the original root.

The transformation follows specific rules:
• For each node on the path from leaf to root (excluding the original root):
• If the current node has a left child, that child becomes its right child
• The current node's original parent becomes its left child
• The original parent's pointer to current node becomes null

Important: You must correctly update all Node.parent pointers in the final tree structure.

Input & Output

example_1.py — Simple Tree Flip
$ Input: root = [3,5,1,6,2,0,8,null,null,7,4], leaf = Node(7)
Output: New tree with 7 as root: [7,2,null,5,4,3,null,6,null,1,null,null,null,0,8]
💡 Note: The leaf node 7 becomes the new root. Its original parent 2 becomes its left child, and the path continues flipping relationships up to the original root 3.
example_2.py — Deep Leaf
$ Input: root = [1,2,3,4,5], leaf = Node(4)
Output: New tree with 4 as root: [4,2,null,1,null,null,3,null,5]
💡 Note: Node 4 (originally leftmost leaf) becomes root, with 2 as left child and 1 as grandchild, while 3 and 5 remain in their relative positions.
example_3.py — Single Child Path
$ Input: root = [1,2,null,3], leaf = Node(3)
Output: New tree with 3 as root: [3,2,null,1]
💡 Note: Simple linear transformation where 3 becomes root, 2 becomes its left child, and 1 becomes the left child of 2.

Constraints

  • The number of nodes in the tree is in the range [1, 100]
  • 1 ≤ Node.val ≤ 100
  • All Node.val are unique
  • leaf exist in the tree and is a leaf node
  • The tree has proper parent pointers set up initially

Visualization

Tap to expand
Change the Root of a Binary Tree INPUT Original Binary Tree (root=3) 3 5 1 6 2 0 8 7 4 leaf=7 root = [3,5,1,6,2,0,8, null,null,7,4] leaf = Node(7) ALGORITHM STEPS 1 Find Path Trace: 7 --> 2 --> 5 --> 3 2 Reverse Links For each node on path: parent becomes left child 3 Handle Children Left child moves to right Old parent link = null 4 Update Parents Update all Node.parent pointers correctly Transformation Process 7 2 5 3 7 becomes new root 2 becomes 7's left child 5 becomes 2's left child 3 becomes 5's left child FINAL RESULT Rerooted Tree (new root=7) 7 2 5 4 3 null 6 1 0 8 Key Insight: Rerooting reverses parent-child relationships along the path from leaf to original root. For each node: (1) Left child moves to right, (2) Parent becomes new left child, (3) All Node.parent pointers must be updated to maintain tree consistency. Time: O(h), Space: O(1) TutorialsPoint - Change the Root of a Binary Tree | Optimal Solution
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18 Apple 15
23.5K Views
Medium Frequency
~25 min Avg. Time
847 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