FunctionRacerProblemsRace

Build a BST by Inserting Values

EasyTreesPython, JavaScript, TypeScript, Java, C++

Start with an empty tree and insert the items of values one at a time, in the given order. Each insertion walks down from the root (left if the new value is smaller than the node, right if larger) and attaches a new leaf where it falls off the tree. Return the final root. The values are distinct. An empty list 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

values [5,3,8,1]
returns [5,3,8,1]

5 is the root; 3 goes left, 8 right, 1 goes left of 3: [5,3,8,1].

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