# Problem 298: Binary Tree Longest Consecutive Sequence

> <https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/>

![](/files/-Lpv9vH0u3L9QXuRwJsd)

## 思路

* 涉及到二叉树的题目一般情况都要进行遍历（或者分治）。
* 可以用一个全局变量来维护 max 值，然后用一个 cur 变量来 track 进行到了哪一步。最后取最大值。

## 复杂度

* Time: $$O(n)$$
* Space: $$O(n)$$

```java
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    int max = 0;
    public int longestConsecutive(TreeNode root) {
        if (root == null) {
            return 0;
        }

        helper(root, 0, root.val);
        return max;
    }

    private void helper(TreeNode root, int cur, int target) {
        if (root == null) {
            return;
        }

        if (root.val == target) {
            cur++;
        } else {
            cur = 1;
        }
        max = Math.max(max, cur);

        helper(root.left, cur, root.val + 1);
        helper(root.right, cur, root.val + 1);
    }
}
```

## 易错点

1. 递归函数的 parameters 都是变量，不能放常量，比如 target

   ```java
   helper(root.left, cur, root.val + 1);
   ```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://liuyang89116.gitbook.io/my-leetcode-book/chapter_3_binary_tree_bfs-_dfs/problem_298_binary_tree_longest_consecutive_sequen.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
