Given the root of a binary tree, return the length of the longest consecutive sequence path.
A consecutive sequence path is a path where the values increase by one along the path.
Note that the path can start at any node in the tree, and you cannot go from a node to its parent in the path.
Example 1:
Input: root = [1,null,3,2,4,null,null,null,5] Output: 3 Explanation: Longest consecutive sequence path is 3-4-5, so return 3.
Example 2:
Input: root = [2,null,3,2,null,1] Output: 2 Explanation: Longest consecutive sequence path is 2-3, not 3-2-1, so return 2.
Constraints:
[1, 3 * 104].-3 * 104 <= Node.val <= 3 * 104Problem Overview: Given a binary tree, return the length of the longest path where each node value is exactly one greater than its parent. The sequence must follow parent-to-child direction only; it cannot change direction or skip nodes.
This problem focuses on recognizing patterns during tree traversal. At each node, you check whether the value continues a consecutive sequence from its parent. The goal is to track the longest such chain across the entire tree.
Approach 1: Start DFS from Every Node (Brute Force) (Time: O(n^2), Space: O(h))
A straightforward strategy is to treat every node as the start of a potential sequence. For each node, run a DFS and continue exploring children only if the child value equals node.val + 1. Track the length of that chain and update the global maximum. Because a DFS can run from every node and may visit many overlapping subtrees repeatedly, the worst-case time complexity becomes O(n^2) for skewed trees. Space complexity is O(h), where h is the tree height due to recursion stack.
Approach 2: Single DFS with Parent Tracking (Optimal) (Time: O(n), Space: O(h))
The optimal solution performs one depth-first search across the tree while carrying two pieces of state: the previous node value and the current consecutive length. When visiting a node, compare node.val with parent.val + 1. If it matches, extend the current sequence. Otherwise, reset the length to 1. Update a global maximum during traversal. Each node is processed exactly once, which gives O(n) time complexity.
This approach works well because consecutive validation only depends on the immediate parent. You don't need to recompute sequences starting from every node; the running length naturally propagates through recursion. The recursion depth determines memory usage, resulting in O(h) space where h is the height of the binary tree.
Implementation insight: Pass the expected next value or the parent value during recursion. When the condition breaks, reset the length and continue exploring children so new sequences can start from that node.
Recommended for interviews: Interviewers typically expect the single DFS solution with parent tracking. The brute-force method demonstrates understanding of tree traversal but wastes work by reprocessing subtrees. The optimized DFS shows that you recognize how sequence state can propagate during recursion, reducing the complexity to linear time.
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| DFS from Every Node (Brute Force) | O(n^2) | O(h) | Good for initial reasoning or when demonstrating basic DFS traversal logic |
| Single DFS with Parent Tracking (Optimal) | O(n) | O(h) | Best general solution; processes each node once and is expected in interviews |
Binary Tree Longest Consecutive Sequence • Kevin Naughton Jr. • 39,003 views views
Watch 9 more video solutions →Practice Binary Tree Longest Consecutive Sequence with our built-in code editor and test cases.
Practice on FleetCode