发新话题
打印

请问能用递归算法复制一棵二叉树吗?

请问能用递归算法复制一棵二叉树吗?

这样行不行:

struct BiNode *Copy(struct BiNode * t)
{struct BiNode *p,*l=NULL,*r=NULL;
if(t)
  {if (t->lchild) l=Copy(t->lchild);
   if (t->rchild) r=Copy(t->rchld);
   p->lchild=l; p->rchild=r;
  }
return(p);
}

到网上搜了半天都只有非递归的....不知到这样行不行

TOP

我顶顶顶顶....

TOP

递归算法肯定可以复制一棵树啊

template <class t>
binarytree<t> * copybinarytree (const binarytree<t>*root)
{
        if (root==null)return null;
        if (root!=null){
    bintreenode<t> *p=new bintreenode<t>(root->data);
    p->leftchild=copy(root->leftchild);
    p->rightchild=copy(root->righttchild);
    return p ;
  }
}

TOP

发新话题

当前时区 GMT+8, 现在时间是 2008-9-5 14:59

蜀ICP备05000763号


清除 Cookies - 联系我们 - 5432考研网 - Archiver - WAP - TOP