Question d’entretien chez Google

Given a generic tree, how would you pick a node at random with uniform probability?

Réponses aux questions d'entretien

Utilisateur anonyme

8 mars 2012

Just pick a number from 1 to N and do an inorder traversal.

2

Utilisateur anonyme

20 oct. 2011

Flatten the tree into an array list, then generate a uniform random number from 0 to 1, and choose the corresponding index in the flattened array list.