Trees and Graphs‎ > ‎

Clone Tree

Clone a binary tree.
Node<T> cloneTree<T>(Node<T> n)
{
    if (n == null)
        return null;
 
    Node<T> c = new Node<T>(n.val);
    c.left = cloneTree<T>(n.left);
    c.right = cloneTree<T>(n.right);
 
    return c;
}
Comments