Increasing BST Order

EasyArrayBinary SearchSortingLinked ListTreeBinary Search Tree

Description

Given the root of a binary search tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root, and every node has no left child and only one right child. Return the result as an array.

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]
Explanation:

In-order right-skewed.

Input:root = [4,2,6,1,3,5,7]
Output:[1,null,2,null,3,null,4,null,5,null,6,null,7]
Explanation:

Perfect binary search tree converted to right-skewed tree. In-order traversal visits nodes 1,2,3,4,5,6,7, so each node becomes the right child of the previous node in sequence.

Input:root = [10,5,15,null,8,12,20]
Output:[5,null,8,null,10,null,12,null,15,null,20]
Explanation:

The in-order traversal is 5, 8, 10, 12, 15, 20, so the tree becomes the exact right-skewed output [5,null,8,null,10,null,12,null,15,null,20].

Constraints

  • 1 ≤ nodes ≤ 100

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!