Increasing Order Search Tree (LeetCode 897): Solution and Explanation

Easy
Stack
Tree
Depth First search
Binary search tree
Binary tree

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

  1. Create a dummy node and set tail = dummy.
  2. visit(node): if node is null, return. Otherwise visit(node.left) first, so every smaller value is placed before node.
  3. Set node.left = None; its left subtree is already on the chain.
  4. Set tail.right = node and tail = node.
  5. visit(node.right). The argument is read before any later link overwrites node.right.
  6. After visit(root), return dummy.right.

Step-by-step example

root = [5,1,7]
  1. visit(5) goes left to visit(1). Node 1 has no left child.
  2. Link 1: 1.left = None, dummy.right = 1, tail = 1. Node 1 has no right child, return.
  3. Back at 5: 5.left = None, 1.right = 5, tail = 5.
  4. visit(7): 7.left = None, 5.right = 7, tail = 7.
  5. 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.right
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each node is visited once and relinked with O(1) pointer changes.

Space

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.