> For the complete documentation index, see [llms.txt](https://protegejj.gitbook.io/my-algorithm-summary/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://protegejj.gitbook.io/my-algorithm-summary/data-structure/tree/298-binary-tree-longest-consecutive-sequence.md).

# 298 Binary Tree Longest Consecutive Sequence

## 298. Binary Tree Longest Consecutive Sequence

## 1. Question

Given a binary tree, find the length of the longest consecutive sequence path.

The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from parent to child (cannot be the reverse).

For example,

```
   1
    \
     3
    / \
   2   4
        \
         5
```

Longest consecutive sequence path is`3-4-5`, so return`3`.

```
   2
    \
     3
    / 
   2    
  / 
 1
```

Longest consecutive sequence path is`2-3`,not`3-2-1`, so return`2`.

## 2. Implementation

**(1) DFS**

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

        int[] maxLen = new int[1];
        getLongestConsecutive(root, root.val + 1, 1, maxLen);
        return maxLen[0];
    }

    public void getLongestConsecutive(TreeNode node, int target, int curLen, int[] maxLen) {
        if (node == null) {
            return;
        }

        if (node.val == target) {
            ++curLen;
        }
        else {
            curLen = 1;
        }

        maxLen[0] = Math.max(maxLen[0], curLen);

        getLongestConsecutive(node.left, node.val + 1, curLen, maxLen);
        getLongestConsecutive(node.right, node.val + 1, curLen, maxLen);
    }
}
```

**(2) BFS**

```java
class Solution {
    public int longestConsecutive(TreeNode root) {
        if (root == null) {
            return 0;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        Queue<Integer> len = new LinkedList<>();

        queue.add(root);
        len.add(1);
        int maxLen = 1;

        while (!queue.isEmpty()) {
            int size = queue.size();

            for (int i = 0; i < size; i++) {
                TreeNode curNode = queue.remove();
                int curLen = len.remove();

                maxLen = Math.max(maxLen, curLen);

                if (curNode.left != null) {
                    queue.add(curNode.left);
                    len.add(curNode.left.val == curNode.val + 1 ? curLen + 1 : 1);
                }

                if (curNode.right != null) {
                    queue.add(curNode.right);
                    len.add(curNode.right.val == curNode.val + 1 ? curLen + 1 : 1);
                }
            }
        }
        return maxLen;
    }
}
```

## 3. Time & Space Complexity

DFS: 时间复杂度: O(n), 空间复杂度: O(h)

BFS: 时间复杂度: O(n), 空间复杂度: O(w), w为树中拥有最多node的一层的node个数


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://protegejj.gitbook.io/my-algorithm-summary/data-structure/tree/298-binary-tree-longest-consecutive-sequence.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
