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.

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.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single in-order traversal visiting each node exactly once

n
2n
โœ“ Linear Growth
Space Complexity
O(h)

Only recursion stack space where h is height of tree. No extra data structures.

n
2n
โœ“ Linear Space

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?
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