按顺序复制二叉树
到目前为止我编写的代码是:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
假设 orgTree 指向根 (
2
)。对于复制,我们必须执行以下操作:copyTree
处创建一个节点,然后 如果orgTree->left != NULL
,则将值 2 复制到其中,copyInOrder( orgTree->left, copyTree->left );
orgTree->right != NULL
,调用copyInOrder( orgTree->right, copyTree->right );
BTW,这种类型的遍历称为 前序遍历,中序遍历不同。
Suppose
orgTree
points to root (2
). For copying, we have to do the following:copyTree
, and the copy the value 2 into itorgTree->left != NULL
, callcopyInOrder( orgTree->left, copyTree->left );
orgTree->right != NULL
, callcopyInOrder( orgTree->right, copyTree->right );
BTW, this type of traversal is known as pre-order traversal, in-order traversal is different.
我想事情会是这样的。
I think it would be something like this.
这是一种有效且简单的递归方法
This is a recursive method that works and is simple
我无权举报或评论。也许获得授权的人会对被劫持的链接采取行动。然后,也许删除我的帖子(这篇文章),因为它不再相关。
参考 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."