Increasing Order Search Tree (LeetCode 897): Solution and Explanation
Rearrange a binary search tree so that the smallest node becomes the root and every node has no left child and only a right child, in increasing order.
Intuition
The finished tree is just a sorted list of the values held together by right pointers. A BST already knows its sorted order: an inorder traversal produces it.
You could collect the values into an array and build fresh nodes, but there is no need. Each node can be moved as the traversal reaches it: clear its left pointer and hang it below the last node placed on the chain.
A dummy node in front of the chain means the first node needs no special case, and a tail pointer always marks where the next node goes. The answer is dummy.right.
Approach: Inorder relinking with a tail pointer
- Create a dummy node and set tail = dummy.
- visit(node): if node is null, return. Otherwise visit(node.left) first, so every smaller value is placed before node.
- Set node.left = None; its left subtree is already on the chain.
- Set tail.right = node and tail = node.
- visit(node.right). The argument is read before any later link overwrites node.right.
- After visit(root), return dummy.right.
Step-by-step example
root = [5,1,7]
- visit(5) goes left to visit(1). Node 1 has no left child.
- Link 1: 1.left = None, dummy.right = 1, tail = 1. Node 1 has no right child, return.
- Back at 5: 5.left = None, 1.right = 5, tail = 5.
- visit(7): 7.left = None, 5.right = 7, tail = 7.
- Return dummy.right = 1, giving [1,null,5,null,7].
Code: Python, Java, C++
1class Solution:
2 def increasingBST(self, root: TreeNode) -> TreeNode:
3 dummy = TreeNode(0)
4 self.tail = dummy
5
6 def visit(node):
7 if not node:
8 return
9 visit(node.left)
10 node.left = None
11 self.tail.right = node
12 self.tail = node
13 visit(node.right)
14
15 visit(root)
16 return dummy.right1class Solution {
2 private TreeNode tail;
3
4 public TreeNode increasingBST(TreeNode root) {
5 TreeNode dummy = new TreeNode(0);
6 tail = dummy;
7 visit(root);
8 return dummy.right;
9 }
10
11 private void visit(TreeNode node) {
12 if (node == null) return;
13 visit(node.left);
14 node.left = null;
15 tail.right = node;
16 tail = node;
17 visit(node.right);
18 }
19}1class Solution {
2 TreeNode* tail;
3
4 void visit(TreeNode* node) {
5 if (!node) return;
6 visit(node->left);
7 node->left = nullptr;
8 tail->right = node;
9 tail = node;
10 visit(node->right);
11 }
12
13public:
14 TreeNode* increasingBST(TreeNode* root) {
15 TreeNode dummy(0);
16 tail = &dummy;
17 visit(root);
18 return dummy.right;
19 }
20};Time and space complexity
O(n)
Each node is visited once and relinked with O(1) pointer changes.
O(h)
Only the recursion stack is used, one frame per level along the current path. h is O(log n) for a balanced tree and O(n) for a chain.
Other approaches
Collect values and rebuild (time O(n), space O(n))
Run an inorder traversal into a list, then create new nodes linked by right pointers. Easy to reason about, but allocates n new nodes.
Iterative inorder with a stack (time O(n), space O(h))
Same relinking, driven by an explicit stack instead of recursion. Read node.right before setting the next link.
Morris traversal (time O(n), space O(1))
Thread predecessors to avoid both recursion and a stack, relinking as nodes are visited. Constant extra space but trickier to get right.
Common mistakes
- Forgetting node.left = None. The output must have no left children, and leaving old left pointers in place creates cycles in the result.
- Returning root instead of dummy.right. The new root is the smallest value, not the old root.
- In an iterative version, overwriting node.right before saving it, which loses the right subtree.
Frequently asked questions
Why does clearing node.left not break the traversal?
By the time node.left is cleared, visit(node.left) has already finished, so the left subtree is no longer needed for the walk.
Is the result still a binary search tree?
Yes. Every node's right subtree holds only larger values, and there are no left subtrees, so the BST property holds. It is just completely unbalanced.
How is this different from Flatten Binary Tree to Linked List?
Flatten uses preorder, so the root comes first. Here the order is inorder, so the chain comes out sorted and the smallest node becomes the new root.