← All problemsSign in

Utkarsh and Random Trees

Utkarsh was recently setting a problem on trees. So he wanted to generate trees of N nodes by his program using the following procedure. - All the nodes are aded one by one to the tree. - 1 is added first as the root of the tree. - 2 is added and 1 is made the parent of 2. - For every node i which is to be added next, one of i-1 or i-2 is seleclted randomly (eqiuprobably) and is made the pare

HINT LADDERno hints yet
L1 Observation
L2 Technique
L3 Approach
L4 Pseudo-code
🔒
L5 Full solution
L5 unlocks only if you insist twice
solution.cppC++17

CodeSearch Tutor

Hints, not spoilers — it won’t hand over the full solution unless you insist.

voice by Sarvam AI

Sign in to chat with the tutor and save your progress.

Sign in to start