FunctionRacerProblemsRace

Longest Consecutive Downward Path

MediumTreesPython, JavaScript, TypeScript, Java, C++

You are given the root of a binary tree. Find the longest downward path (from a parent to a child, repeatedly, starting at any node) in which each value is exactly 1 more than the one before it. Return the number of nodes on it. A single node counts as a path of length 1. An empty tree gives 0.

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 [1,null,3,2,4,null,null,null,5]
returns 3

3 -> 4 -> 5 has three nodes.

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