To prevent multiple occurrences of the same value, you have several weapons at your disposal. The main methods are:
- Use "chalk" (in the form of a list, or hash table) to mark visited nodes. Then check the chalk each time you propose a visitation to another node.
- Look more closesly at the structure of the tree to see exactly what makes each leaf unique. Use this in determining what values must change each permutation/recurse.
The first one is a a thoughtless and powerful fix. It's also generally applicable to many similar situations. And the hash table has an amortized cost of
)
, so it's not inefficient. However, it's not the most elegant solution for this problem.
The second one is more "proper", but I don't understand the problem enough to recommend anything less broad. It seems perfectly possible, and more along the lines of what you're asking for an answer. Could you explain the question further?