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
Increasing Order Search Tree Transform BST to Right-Skewed Tree INPUT: BST 5 3 6 2 4 8 1 7 9 In-order: 1,2,3,4,5,6,7,8,9 (sorted sequence) root=[5,3,6,2,4,null,8,1...] ALGORITHM STEPS 1 Create Dummy Node Initialize current pointer 2 In-order Traversal Visit left, node, right 3 Rebuild Structure Set left=null, attach right 4 Return New Root dummy.right is answer def increasingBST(root): dummy = TreeNode(0) curr = dummy def inorder(node): curr.right = node node.left = None Complexity Time: O(n) | Space: O(h) FINAL RESULT Right-Skewed Tree 1 2 3 4 5 ... 9 Only right child Output [1,null,2,null,3,null,4,null, 5,null,6,null,7,null,8,null,9] Key Insight: BST in-order traversal gives sorted sequence. We traverse in-order and rebuild the tree by: 1) Setting each node's left child to null 2) Linking each node as the right child of the previous node TutorialsPoint - Increasing Order Search Tree | Optimal Solution O(n)
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