FunctionRacerProblemsRace

Values at Depth K

BeginnerTreesPython, JavaScript, TypeScript, Java, C++

The root of the tree has depth 0. Return the values of all nodes at depth k in root, from left to right. If the tree has no nodes at that depth, return [].

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 [3,9,20,null,null,15,7]
k 2
returns [15,7]

Depth 2 holds 15 and 7.

Example 2

root [3,9,20]
k 5
returns []

The tree is not that deep.

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