按顺序复制二叉树

发布于 2024-09-27 08:05:33 字数 318 浏览 2 评论 0原文

到目前为止我编写的代码是:

void copyInOrder(TNode *orgTree, Tnode *& copyTree){
    if(orgTree !=NULL){
        copyInOrder(orgTree->left_link);
        //create leftmost node of tree but how to link to parent
        copyInOrder(orgTree->right_link);
    }
}

我不知道如何将父级链接到节点作为其中序。

The code I wrote so far is:

void copyInOrder(TNode *orgTree, Tnode *& copyTree){
    if(orgTree !=NULL){
        copyInOrder(orgTree->left_link);
        //create leftmost node of tree but how to link to parent
        copyInOrder(orgTree->right_link);
    }
}

I dont know how to link to the parent to the nodes as its inorder.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

国粹 2024-10-04 08:05:33

假设 orgTree 指向根 (2)。对于复制,我们必须执行以下操作:

alt text

  1. copyTree 处创建一个节点,然后 如果 orgTree->left != NULL,则将值 2 复制到其中,
  2. 调用 copyInOrder( orgTree->left, copyTree->left );
  3. if orgTree->right != NULL,调用 copyInOrder( orgTree->right, copyTree->right );

BTW,这种类型的遍历称为 前序遍历,中序遍历不同。

Suppose orgTree points to root (2). For copying, we have to do the following:

alt text

  1. create a node at copyTree, and the copy the value 2 into it
  2. if orgTree->left != NULL, call copyInOrder( orgTree->left, copyTree->left );
  3. if orgTree->right != NULL, call copyInOrder( orgTree->right, copyTree->right );

BTW, this type of traversal is known as pre-order traversal, in-order traversal is different.

心不设防 2024-10-04 08:05:33

我想事情会是这样的。

void copyInOrder(TNode *orgTree, Tnode *& copyTree){
    if(orgTree !=NULL){
        //left side
        TNode newLeftNode = cloneNode(orgTree->left_link);
        copyTree->left_link = newLeftNode;
        copyInOrder(orgTree->left_link, copyTree->left_link);

        //right side
        TNode newRightNode = cloneNode(orgTree->right_link);
        copyTree->right_link = newRightNode;
        copyInOrder(orgTree->right_link, copyTree->right_link);
    }
}

I think it would be something like this.

void copyInOrder(TNode *orgTree, Tnode *& copyTree){
    if(orgTree !=NULL){
        //left side
        TNode newLeftNode = cloneNode(orgTree->left_link);
        copyTree->left_link = newLeftNode;
        copyInOrder(orgTree->left_link, copyTree->left_link);

        //right side
        TNode newRightNode = cloneNode(orgTree->right_link);
        copyTree->right_link = newRightNode;
        copyInOrder(orgTree->right_link, copyTree->right_link);
    }
}
或十年 2024-10-04 08:05:33
tnode *copy(tnode *root) {
     tnode *new_root;
     if(root!=NULL){
         new_root=new tnode;
         new_root->data=root->data;
         new_root->lchild=copy(root->lchild);
         new_root->rchild=copy(root->rchild);
     } else return NULL;
     return new_root;
 }
tnode *copy(tnode *root) {
     tnode *new_root;
     if(root!=NULL){
         new_root=new tnode;
         new_root->data=root->data;
         new_root->lchild=copy(root->lchild);
         new_root->rchild=copy(root->rchild);
     } else return NULL;
     return new_root;
 }
雨落□心尘 2024-10-04 08:05:33

这是一种有效且简单的递归方法

Tnode* CopyInOrder(Tnode* root){
    if(root == NULL){return NULL;}
    else{
        Tnode* temp = new Tnode;
        temp -> data = root -> data;
        temp -> left = copyInOrder(root -> left);
        temp -> right = copyInOrder(root -> right);
        return temp;
        }
}

This is a recursive method that works and is simple

Tnode* CopyInOrder(Tnode* root){
    if(root == NULL){return NULL;}
    else{
        Tnode* temp = new Tnode;
        temp -> data = root -> data;
        temp -> left = copyInOrder(root -> left);
        temp -> right = copyInOrder(root -> right);
        return temp;
        }
}
垂暮老矣 2024-10-04 08:05:33

我无权举报或评论。也许获得授权的人会对被劫持的链接采取行动。然后,也许删除我的帖子(这篇文章),因为它不再相关。

参考 Arun 于 2010 年 10 月 12 日 21:02 发表的帖子:

我检查了该链接(即右键单击,然后单击“检查”)。该 URL 看起来合法。然而,当我实际单击该链接时,我会被重定向到一个完全不同的 URL,该网站想要下载并安装插件。诺顿安全阻止该网站。对我来说,原始链接似乎被劫持了。

原始帖子显示了知识和帮助。

为了安全起见,也许我们应该改为谷歌搜索词“前序遍历”。

I am not authorized to flag or comment. Maybe someone who is authorized will take action on the hijacked link. And then, maybe delete my post (this post) since it will no longer be relevant.

Referring to the post of Oct 12 '10 at 21:02 by Arun:

I inspected the link (i.e., right-click, then click on "inspect"). The URL looks legitimate. However, when I actually click on the link, I get redirected to a completely different URL of a website that wants to download and install a plugin. Norton Security blocks the website. To me, it looks like the original link was hijacked.

The original post shows knowledge and helpfulness.

For safety, maybe we should change to google the search term "pre-order traversal."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文