# 549 Binary Tree Longest Consecutive Sequence II

## 549. Binary Tree Longest Consecutive Sequence II

## 1. Question

Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree.

Especially, 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:

        1
       / \
      2   3

Output:
 2

Explanation:
 The longest consecutive path is [1, 2] or [2, 1].
```

**Example 2:**

```
Input:

        2
       / \
      1   3

Output:
 3

Explanation:
 The longest consecutive path is [1, 2, 3] or [3, 2, 1].
```

**Note:**&#x41;ll the values of tree nodes are in the range of \[-1e7, 1e7].

## 2. Implementation

**(1) DFS**

思路：对于每个node,我们分别找出它的左子树和右子树中最长的increasing consecutive sequence的长度和decreasing consecutive sequence的长度, 然后这两个长度加起来减一(该node被重复算了一次)，则是穿过这个node的最长consecutive sequence，通过这个方法，递归地遍历每个node可以得到最后的结果

```java
class Solution {
    public int longestConsecutive(TreeNode root) {
        int[] max = new int[1];
        dfs(root, max);
        return max[0];
    }

    public int[] dfs(TreeNode node, int[] max) {
        if (node == null) {
            return new int[] {0, 0};
        }

        int decrease = 1, increase = 1;

        if (node.left != null) {
            int[] left = dfs(node.left, max);
            if (node.val - 1 == node.left.val) {
                decrease = left[0] + 1;
            }
            else if (node.val + 1 == node.left.val) {
                increase = left[1] + 1;
            }
        }

        if (node.right != null) {
            int[] right = dfs(node.right, max);
            if (node.val - 1 == node.right.val) {
                decrease = Math.max(decrease, right[0] + 1);
            }
            else if (node.val + 1 == node.right.val) {
                increase = Math.max(increase, right[1] + 1);
            }
        }

        max[0] = Math.max(max[0], decrease + increase - 1);
        return new int[] {decrease, increase};
    }
}
```

## 3. Time & Space Complexity

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


---

# 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://protegejj.gitbook.io/my-algorithm-summary/data-structure/tree/549-binary-tree-longest-consecutive-sequence-ii.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.
