```
int longest=0;
void longestC(TreeNode* root, int value, int curlong)
{
if(root==NULL)
return;
if(root->val - value==1)
{
curlong++;
if(curlong>longest)
longest=curlong;
}
else curlong=0;
longestC(root->left,root->val,curlong);
longestC(root->right,root->val,curlong);
}
int longestConsecutive(TreeNode* root) {
if(root==NULL) return 0;
longestC(root,root->val,0);
return longest+1;
}
```