```
public int LongestConsecutive(TreeNode root) {
return Longest(root, 0, 0);
}
public int Longest(TreeNode node, int curr, int len)
{
if (node == null) return len;
int m = node.val == curr + 1 ? len + 1 : 1;
int p = node.val == curr + 1 ? len + 1 : len;
int left = Longest(node.left, node.val, m);
int right = Longest(node.right, node.val, m);
return Math.Max(p, Math.Max(left, right));
}
```