I wrote an ordinary python code as follows

```
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def longestConsecutive(self, root):
"""
:type root: TreeNode
:rtype: int
"""
self.maxnum = 1
if root:
self.passingdata(root, 1)
else:
return 0
return self.maxnum
def passingdata(self, root, longestCurrent):
currentlongest = longestCurrent
if root.left:
if root.val + 1 == root.left.val:
currentlongest += 1
self.maxnum = max(currentlongest, self.maxnum)
self.passingdata(root.left, currentlongest)
else:
self.passingdata(root.left, 1)
currentlongest = longestCurrent
if root.right:
if root.val + 1 == root.right.val:
currentlongest += 1
self.maxnum = max(currentlongest, self.maxnum)
self.passingdata(root.right, currentlongest)
else:
self.passingdata(root.right, 1)
```