FunctionRacerProblemsRace

Maximum Binary Tree

MediumTreesPython, JavaScript, TypeScript, Java, C++

Build a tree from nums (distinct values) like this: the root is the largest value; its left subtree is built the same way from the items to the left of that value in the list, and its right subtree from the items to the right. Return the root. 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

nums [3,2,1,6,0,5]
returns [6,3,5,null,2,0,null,null,1]

Root 6; left from [3,2,1] (root 3, right child 2, whose right child is 1); right from [0,5].

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