Simple recursive sol Python

import sys

class Solution:
# @param A : root node of tree
# @param B : integer
# @return an integer

def gep(self,node,h,o,arr):

    if node == None:
        return False,0,0

    a,b,c = self.gep(node.left,h,o+1,arr)        
    x,y,z = self.gep(node.right,h,o+1,arr) 

    if node.val == h:            
        arr[0] = max(arr[0],max(c,z)+1,o-1)
        return True,0,max(c,z)+1

    elif a:
        arr[0] = max(arr[0],z+b)            
        return True,b+1,max(c,z)+1

    elif x:
        arr[0] = max(arr[0],c+y)
        return True,y+1,max(c,z)+1

    return False,0,max(c,z)+1

def solve(self, A, B):

    arr = [0]
    a,b,c = self.gep(A,B,0,arr)
    return arr[0]+1
Click here to start solving coding interview questions