# Question

Formatted question description: https://leetcode.ca/all/298.html

 298.Binary Tree Longest Consecutive Sequence

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.

@tag-tree



# Algorithm

If the left child node exists and the node value is greater than the value of its parent node by 1, the function is called recursively.

If the node value is not exactly greater than 1, the function with the length reset is recursively called. The processing of the right child node and the left child node the same.

# Code

Java

• 
public class Binary_Tree_Longest_Consecutive_Sequence {
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {

private int maxLen = 0;

public int longestConsecutive(TreeNode root) {
longestConsecutive(root, 0, 0);
return maxLen;
}

private void longestConsecutive(TreeNode root, int lastVal, int curLen) {
if (root == null) {
return;
}
if (root.val != lastVal + 1) {
curLen = 1;
} else {
curLen++;
}

maxLen = Math.max(maxLen, curLen);

longestConsecutive(root.left, root.val, curLen);
longestConsecutive(root.right, root.val, curLen);
}
}

// Top-down DFS
class Solution_topdown {
public int longestConsecutive(TreeNode root) {
return getLongestConsecutive(root, null, 0);
}

private int getLongestConsecutive(TreeNode child, TreeNode parent, int len) {
if (child == null) return 0;
len = (parent != null && parent.val == child.val - 1) ? len + 1 : 1;
return Math.max(len,
Math.max(
getLongestConsecutive(child.left, child, len),
getLongestConsecutive(child.right, child, len)
)
);
}
}

// Bottom-up DFS
public class Solution_bottomUp {

int result = 0;
public int longestConsecutive(TreeNode root) {
if (root == null) {
return 0;
}
getLongestConsecutive(root);
return result;
}

private int getLongestConsecutive(TreeNode root) {
int ret = 1;

if (root.left != null) {
int left = getLongestConsecutive(root.left);
if (root.val == root.left.val - 1) {
ret = Math.max(ret, 1 + left);
}
}

if (root.right != null) {
int right = getLongestConsecutive(root.right);
if (root.val == root.right.val - 1) {
ret = Math.max(ret, 1 + right);
}
}

result = Math.max(result, ret);
return ret;
}

}
}


• // OJ: https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/
// Time: O(N)
// Space: O(H)
class Solution {
int ans = 0;
void dfs(TreeNode *root, TreeNode *parent = NULL, int length = 1) {
if (!root) return;
length = parent && parent->val + 1 == root->val ? length + 1 : 1;
ans = max(ans, length);
dfs(root->left, root, length);
dfs(root->right, root, length);
}
public:
int longestConsecutive(TreeNode* root) {
dfs(root);
return ans;
}
};

• # Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
def longestConsecutive(self, root: TreeNode) -> int:
def dfs(root, p, t):
nonlocal ans
if root is None:
return
t = t + 1 if p is not None and p.val + 1 == root.val else 1
ans = max(ans, t)
dfs(root.left, root, t)
dfs(root.right, root, t)

ans = 1
dfs(root, None, 1)
return ans

############

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
def longestConsecutive(self, root):
"""
:type root: TreeNode
:rtype: int
"""

def helper(root):
if not root:
return (None, 0, 0)  # (val, consecutive len, max consecutive len)
left, leftLen, maxLeftLen = helper(root.left)
right, rightLen, maxRightLen = helper(root.right)
ret = 1
if root.val + 1 == left:
ret = leftLen + 1
if root.val + 1 == right:
ret = max(ret, rightLen + 1)
return (root.val, ret, max(maxLeftLen, maxRightLen, ret))

return helper(root)