Make delicious recipes!

Clone graph recursively

Puzzle: Given a graph (where each node has a variable number of children), write a function to recursively clone it.


Node clone (Node orig)
    Node c = new Node ();
    clone (orig, c);
    return c;

void clone(Node orig, Node c) 
{ =;
    c.children = new ArrayList <Node> (orig.children.size());

    for (int i=0; i<orig.children.size(); i++)

Like us on Facebook to remain in touch
with the latest in technology and tutorials!

Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Email: (Your email is not shared with anybody)

Facebook comments:

Site Owner: Sachin Goyal