FunctionRacerProblemsRace

Check a Root-to-Leaf Sequence

MediumTreesPython, JavaScript, TypeScript, Java, C++

You are given the root of a binary tree. Given a list arr, return True if there is a path from the root down to a leaf whose values, read from the top, are exactly arr (same values, same order, same length). Otherwise return False.

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 [0,1,0,0,1,0,null,null,1,0,0]
arr [0,1,0,1]
returns true

The path 0, 1, 0, 1 ends at a leaf.

Example 2

root [0,1,0,0,1,0,null,null,1,0,0]
arr [0,0,1]
returns false

The path 0, 0, 1 stops at a node that is not a leaf.

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