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 path where the values along the nodes form a consecutive sequence (e.g., 1, 2, 3, ...). The path can go from parent to child or from child to parent - it can traverse through any connected nodes in the tree.
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,2,3]
Output: 2
Input: [2,1,3]
Output: 3
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).
Related Problems
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.