FunctionRacerProblemsRace

Increasing Order Search Tree

EasyTreesPython, JavaScript, TypeScript, Java, C++

You are given the root of a binary tree. Rearrange the BST into a single chain going to the right: the smallest value is the root, it has no left child and its right child is the next smallest, and so on up to the largest. Every node has no left child. Return the root of the chain. An empty tree gives an empty tree.

Trees appear in the tests in level order, with null for a missing child, like [3,9,20,null,null,15,7]. Each node is a TreeNode with .val, .left and .right; the class is already defined for you.

Example 1

root [5,3,6,2,4,null,8]
returns [2,null,3,null,4,null,5,null,6,null,8]

The chain is 2, 3, 4, 5, 6, 8, i.e. [2,None,3,None,4,None,5,None,6,None,8].

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Trees problems