```
def longestConsecutive(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if root is None: return 0
return self.recur(root, 1, 1, root.val)
def recur(self, node, curr_count, max_count, prev):
if node.val-prev == 1:
curr_count += 1
else:
curr_count = 1
left_count = 1
right_count = 1
if node.left is not None:
left_count = self.recur(node.left, curr_count, max_count, node.val)
if node.right is not None:
right_count = self.recur(node.right, curr_count, max_count, node.val)
if node.left is None and node.right is None:
return curr_count
else:
max_count = max(left_count, right_count, max_count, curr_count)
return max_count
```