Utility function to count subtress that sum up to a given value x
def countSubtreesWithSumXUtil ( root , x ) : NEW_LINE INDENT global count , ptr NEW_LINE l = 0 NEW_LINE r = 0 NEW_LINE if ( root == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT l += countSubtreesWithSumXUtil ( root . left , x ) NEW_LINE r += countSubtreesWithSumXUtil ( root . right , x ) NEW_LINE if ( l + r + root . data == x ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT if ( ptr != root ) : NEW_LINE INDENT return l + root . data + r NEW_LINE DEDENT return count NEW_LINE DEDENT