Longest Consecutive Downward Path
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
0 <= number of nodes <= 100-100 <= node.val <= 100
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Trees problems
- Longest Same-Value PathMedium
- Lowest Common Ancestor of a Binary TreeMedium
- Maximum Binary TreeMedium
- Maximum Width of Binary TreeMedium
- Most Frequent Subtree SumMedium
- Nodes Equal to Subtree AverageMedium
- Path Sum IIMedium
- Path Sum IIIMedium