We traverse the tree from root to leaves, passing down the current consecutive sequence length to each child. At each node, we check if it continues the sequence from its parent (value is exactly one more than the parent). If so, we extend the sequence; otherwise, we start a new sequence. We track the maximum length seen across all nodes.
null, return.1, increment the length; otherwise, reset it to 1.class Solution:
def longestConsecutive(self, root: Optional[TreeNode]) -> int:
self.max_length = 0
self.dfs(root, None, 0)
return self.max_length
def dfs(self, p: TreeNode, parent: TreeNode, length: int) -> None:
if p is None:
return
length = length + 1 if parent is not None and p.val == parent.val + 1 else 1
self.max_length = max(self.max_length, length)
self.dfs(p.left, p, length)
self.dfs(p.right, p, length)Where is the number of nodes in the input tree
Instead of passing information down, we can compute the consecutive sequence length from leaves up to the root. Each node returns the length of the longest consecutive sequence starting from itself going downward. The parent then checks if it can extend this sequence by verifying that its value is exactly one less than its child's value.
null, return 0.1 for the current node.current value + 1 != left value), reset the left length to 1.class Solution:
def longestConsecutive(self, root: Optional[TreeNode]) -> int:
self.max_length = 0
self.dfs(root)
return self.max_length
def dfs(self, p: Optional[TreeNode]) -> int:
if p is None:
return 0
L = self.dfs(p.left) + 1
R = self.dfs(p.right) + 1
if p.left is not None and p.val + 1 != p.left.val:
L = 1
if p.right is not None and p.val + 1 != p.right.val:
R = 1
length = max(L, R)
self.max_length = max(self.max_length, length)
return lengthWhere is the number of nodes in the input tree
The problem asks for strictly increasing consecutive sequences where each child is exactly parent.val + 1. A common mistake is checking for any consecutive values (including decreasing) or allowing gaps.
# Wrong: allows decreasing sequences
if abs(p.val - parent.val) == 1:When the consecutive pattern breaks, the length must reset to 1 (the current node starts a new sequence). Forgetting to reset causes incorrect counts.
# Wrong: continues accumulating instead of resetting
length = length + 1 # should be: length = 1 when pattern breaksThe root node has no parent, so attempting to check parent.val without a null check causes errors. The root always starts a new sequence of length 1.