Domanda di colloquio di Google

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

Risposte di colloquio

Anonimo

8 mar 2012

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

2

Anonimo

20 ott 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.