Increasing Order Search Tree - Problem
Transform a Binary Search Tree into a Skewed Right Tree
Given the
🎯 Goal: Create a "skewed" tree where every node only has a right child
📋 Requirements:
• The leftmost node becomes the new root
• Every node has no left child
• Every node has at most one right child
• The tree maintains in-order traversal sequence
Example: If your BST in-order is [1, 5, 3, 6, 2, 8, 7], the result should be a right-skewed tree: 1 → 5 → 3 → 6 → 2 → 8 → 7
💡 Key Insight: Since BST in-order traversal gives sorted sequence, we need to create a linear right-only chain in that exact order!
Given the
root of a binary search tree, your task is to rearrange the tree structure so that it follows these rules:🎯 Goal: Create a "skewed" tree where every node only has a right child
📋 Requirements:
• The leftmost node becomes the new root
• Every node has no left child
• Every node has at most one right child
• The tree maintains in-order traversal sequence
Example: If your BST in-order is [1, 5, 3, 6, 2, 8, 7], the result should be a right-skewed tree: 1 → 5 → 3 → 6 → 2 → 8 → 7
💡 Key Insight: Since BST in-order traversal gives sorted sequence, we need to create a linear right-only chain in that exact order!
Input & Output
example_1.py — Basic BST
$
Input:
root = [5,3,6,2,4,null,8,1,null,null,null,7,9]
›
Output:
[1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]
💡 Note:
The in-order traversal of the original BST gives us [1,2,3,4,5,6,7,8,9]. We rearrange this into a right-skewed tree where each node only has a right child, maintaining the sorted order.
example_2.py — Simple Tree
$
Input:
root = [5,1,7]
›
Output:
[1,null,5,null,7]
💡 Note:
In-order traversal gives [1,5,7]. The result is a chain: 1→5→7 where 1 is the root, 1.right=5, 5.right=7, and all left pointers are null.
example_3.py — Single Node
$
Input:
root = [1]
›
Output:
[1]
💡 Note:
A single node tree remains unchanged since it already has no left children and satisfies the constraint.
Constraints
-
The number of nodes in the given tree will be in the range
[1, 100] -
0 ≤ Node.val ≤ 1000 - The tree is guaranteed to be a valid binary search tree
- Follow-up: Can you solve this in O(h) space where h is the height of the tree?
Visualization
Tap to expand
Understanding the Visualization
1
Identify Traversal Order
In-order traversal of BST gives us the sorted sequence we need
2
Use Previous Pointer
Maintain reference to previously processed node to build chain
3
Modify During Traversal
Clear left pointers and connect right pointers as we visit nodes
4
Return New Root
The leftmost node becomes root of our right-skewed tree
Key Takeaway
🎯 Key Insight: In-order traversal of a BST naturally gives us the sorted sequence we need. Instead of collecting values and rebuilding, we can modify the tree structure during traversal using a previous node pointer, achieving optimal space complexity.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code