Increasing Order Search Tree - Problem
Transform a Binary Search Tree into a Skewed Right Tree

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
BST to Right-Skewed Tree TransformationOriginal BST536148Right-Skewed Result134568🎯 Key InsightIn-order traversal of BST gives sorted order → Perfect for building right-skewed chain!Use previous pointer during traversal to connect nodes without extra space
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.
Asked in
Amazon 45 Google 35 Microsoft 28 Meta 22
98.5K Views
Medium Frequency
~15 min Avg. Time
2.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