FunctionRacerProblemsRace

Build a BST from Preorder

MediumTreesPython, JavaScript, TypeScript, Java, C++

The list preorder is the preorder traversal (node, left subtree, right subtree) of a binary search tree with distinct values. Rebuild that tree and return its root.

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

preorder [8,5,1,7,10,12]
returns [8,5,10,1,7,null,12]

Root 8; the smaller values 5,1,7 form the left subtree, 10,12 the right.

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