Binary Tree Longest Consecutive Sequence II Problem
Description
LeetCode Problem 549.
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 of the consecutive nodes in the path differ by one. This path can be either increasing or decreasing.
- For example,
[1,2,3,4]and[4,3,2,1]are both considered valid, but the path[1,2,4,3]is not valid.
On the other hand, the path can be in the child-Parent-child order, where not necessarily be parent-child order.
Example 1:

Input: root = [1,2,3]
Output: 2
Explanation: The longest consecutive path is [1, 2] or [2, 1].
Example 2:

Input: root = [2,1,3]
Output: 3
Explanation: The longest consecutive path is [1, 2, 3] or [3, 2, 1].
Constraints:
- The number of nodes in the tree is in the range
[1, 3 * 10^4]. -3 * 10^4 <= Node.val <= 3 * 10^4
Solution
Approach: Post-Order DFS with Two Lengths per Node
Any path in a tree has one highest node, where it "bends": from there it goes down into at most two subtrees. For a consecutive path, read it from one end to the other through that top node: the values change by one at every step. Since the top node's value sits in the middle of the sequence, one side going down must decrease step by step (v, v-1, v-2, ...) and the other side must increase (v, v+1, v+2, ...).
So for every node we compute two numbers, bottom-up:
dec: the length of the longest downward path that starts at this node and whose values gov, v-1, v-2, ...inc: the same with valuesv, v+1, v+2, ...
Both start at 1 (the node alone). For each child: if child.val == v - 1, the child's dec path can be extended by this node, so dec = max(dec, child_dec + 1); if child.val == v + 1, likewise for inc. Any other child cannot continue a consecutive path through this node.
The longest consecutive path whose top is this node is then dec + inc - 1: the decreasing side, the increasing side, and the node itself, which both counted. Take the maximum over all nodes.
The two sides always come from different children, since one child cannot be both v - 1 and v + 1. And a path like [1, 2, 3] that does not bend is covered too: one side is just the node itself (length 1). In Example 2, the root 2 has dec = 2 (through 1) and inc = 2 (through 3), so the answer is 2 + 2 - 1 = 3.
Complexity: O(n) time: each node is visited once. O(h) space for the recursion, where h is the height of the tree.
class Solution:
def longestConsecutive(self, root: Optional[TreeNode]) -> int:
best = 0
# Returns (dec, inc): the longest downward paths from node whose values go
# node.val, node.val - 1, ... (dec) and node.val, node.val + 1, ... (inc).
def dfs(node):
nonlocal best
dec = inc = 1
for child in (node.left, node.right):
if child is None:
continue
child_dec, child_inc = dfs(child)
if child.val == node.val - 1:
dec = max(dec, child_dec + 1)
elif child.val == node.val + 1:
inc = max(inc, child_inc + 1)
# the path bends here: down one side decreasing, down the other increasing
best = max(best, dec + inc - 1)
return dec, inc
dfs(root)
return best