```
public int longestConsecutive(TreeNode root) {
if(root == null)
return 0;
if(root.left == null && root.right == null)
return 1;
int left = 0, right = 0;
if(root.left != null){
if(root.val + 1 == root.left.val)
left = longestConsecutive(root.left) + 1;
else
left = longestConsecutive(root.left);
}
if(root.right != null){
if(root.val + 1 == root.right.val)
right = longestConsecutive(root.right) + 1;
else
right = longestConsecutive(root.right);
}
return Math.max(right, left);
}
```

Failed two test cases, can't find the reason, anyone can help?