FunctionRacerProblemsRace

Tree From an Outline

HardTreesPython, JavaScript, TypeScript, Java, C++

outline describes a binary tree as an outline with dots. The nodes are written in this order: a node, then everything under its left child, then everything under its right child. Each node is written as one dot for every level it lies below the root, followed by its value. The entries are separated by single spaces.

A node with only one child has a left child.

Build the 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

outline "8 .3 ..1 ..6 .10"
returns [8,3,10,1,6]

The root 8 has the children 3 and 10. The entries with two dots, 1 and 6, come right after 3, so they are its children.

Example 2

outline "1 .2 ..3 ...4"
returns [1,2,null,3,null,4]

Each entry lies one level deeper than the one before, and an only child goes to the left.

Example 3

outline "7"
returns [7]

A single node.

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