```
public class Solution {
public int longestConsecutive(TreeNode root) {
return longestConsecutiveHelper(root, -1, 0);
}
private int longestConsecutiveHelper(TreeNode node, int prev, int curLen) {
if (node == null) return curLen;
// reset length of path if this is not a consecutive number
if (node.val != prev+1) curLen = 0;
curLen++;
int lenLeftSubTree = longestConsecutiveHelper(node.left, node.val, curLen);
int lenRightSubTree = longestConsecutiveHelper(node.right, node.val, curLen);
// return the longest path available either using this node or available further down
return Math.max(curLen, Math.max(lenLeftSubTree, lenRightSubTree));
}
}
```