549. Binary Tree Longest Consecutive Sequence II
Given the root of a binary tree, return the length of the longest consecutive path in the tree.
A consecutive path is a simple path whose values are globally increasing by exactly 1 or globally decreasing by exactly 1 when read from one endpoint to the other. The path may run from a parent to a child or from a child to a parent, and may pass through a parent between two child subtrees. A change in value-difference sign is not allowed, so 1, 2, 3, 2 is not a valid full consecutive path.
The path does not need to start or end at the root or a leaf; it can be any path in the tree where values are consecutive.
Examples
Input: [1]
Output: 1
Input: [1,2]
Output: 2
Hints
Use DFS from each node, computing both the length of consecutive path going down (increasing) and going up (decreasing) from that node.
For each node, track two values: the longest increasing path going down from this node, and the longest decreasing path going up to this node.
The answer for a node is the sum of its increasing and decreasing paths minus 1 (to avoid double counting the current node).
549. Binary Tree Longest Consecutive Sequence II
Given the root of a binary tree, return the length of the longest consecutive path in the tree.