Increasing Order Search Tree

EasyTreesbst

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given the root of a binary search tree with distinct values. Rearrange it so that the nodes form a single chain in ascending order: the smallest value becomes the new root, every node has **no left child**, and each node's right child is the next larger value. Return the root of the rearranged tree. You may relink the existing nodes or build new ones; only the resulting shape and values are checked. An in-order traversal visits the values in exactly the order the chain needs, so O(n) time is achievable.

Examples

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]
In-order the values are 1..9; each becomes the right child of the previous one.
Input: root = [5,1,7]
Output: [1,null,5,null,7]
The chain is 1 -> 5 -> 7 going right.

Constraints

  • 1 <= number of nodes <= 100
  • 0 <= Node.val <= 1000
  • All values are distinct and the input is a valid BST

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More bst problems