FunctionRacerProblemsRace

Depth of a Value

EasyTreesPython, JavaScript, TypeScript, Java, C++

You are given the root of a binary tree. The root has depth 0, its children depth 1, and so on. Return the depth of the node whose value is target, or -1 if no node has that value. All values are distinct.

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]
target 15
returns 2

3 -> 20 -> 15 is two steps down.

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