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.
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.
Time & Space Complexity
Time Complexity
O(n)
Single in-order traversal visiting each node exactly once
โ Linear Growth
Space Complexity
O(h)
Only recursion stack space where h is height of tree. No extra data structures.
โ 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?
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code