Permutation of phone numbers starting at button x, ending at button y, and z numbers long
One probable solution could be to modify the code as this:


Have the code inside answer in another function, say, isNotTraversalCandidate which returns 0,1 if it the conditions confirm that no further traversal is required else it returns -1.
In recAnswer() use this function, isNotTraversalCandidate(), for the input x, y, z in every level. The recursive function should also verify the x, y, z with those conditions.