Randomly choose n leaf nodes. Return the tree that contains those leaf nodes and their ancestors.
Whenever you say "random", you should specify what probability distribution you want. Unless you nail down the probability distribution, any of these "solutions" will meet your needs:
- Always pick the leftmost n leaves.
- Always pick the rightmost n leaves.
- Always pick the middle n leaves.
- Visit all the leaves in some desired order. Put each one at the end of a list. If the list now contains more than n nodes, delete one of them at random.
- Make a list of all the leaves, and then repeatedly discard one at random until there are only n left.
- Choose a random number from 1 to 5. Use the corresponding method from the above list.
Notice that the word "random" appears several times in the above. Each of these instances of "random" should be qualified by its probability distribution.
But especially notice that randomly picking n items from a larger set is a completely separate sub-problem that has nothing to do with trees. First you solve that, then you come back to the trees domain to build the tree that has your chosen leaf nodes.
Try solving each of these sub-problems. I think you'll find that neither is terribly difficult, once you put the other sub-problem temporarily out of your mind.
Otherwise, if you're trying to build the tree while you're picking the leaves to go into it, you're going to drive yourself mad.