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
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
Important: You must correctly update all
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
nullImportant: 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
Understanding the Visualization
1
Identify the Path
Find the path from the target leaf up to the current root - this is our transformation path
2
Start from Leaf
Begin at the leaf node that will become our new root
3
Flip Relationships
For each step upward, make the parent become the left child of the current node
4
Preserve Structure
Move any existing left child to the right position to maintain tree structure
5
Update Pointers
Ensure all parent pointers are correctly updated throughout the transformation
Key Takeaway
🎯 Key Insight: Only the nodes on the path from leaf to root need to be modified. The rest of the tree structure remains intact, making this an efficient O(h) operation where h is the path length.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code