It passes 52/54 tests:

```
class Solution(object):
def longestConsecutive(self, root):
if root is None:
return 0
return 1 + dfs(root)
def dfs(root):
if root is None:
return 0
a, b = 0,0
if root.left:
if root.left.val - root.val == 1:
a = 1 + dfs(root.left)
else:
a = dfs(root.left)
if root.right:
if root.right.val - root.val == 1:
b = 1 + dfs(root.right)
else:
b = dfs(root.right)
return max(a,b)
```