【问题标题】:How to make copy of the tree for the iterative postOrder traversal to protect the Tree如何为迭代 postOrder 遍历复制树以保护树
【发布时间】:2020-01-01 03:58:29
【问题描述】:

迭代 PostOrder 遍历删除整棵树,在 Scala 中我们有:

override def clone():BinarySearchTree=
{
  return new BinarySearchTree(root.clone())
}

然后我们使用复制整个树:

def postorder(tnode:TreeNode) 
{    
   var tcnode=tnode.clone
}

现在 tcnode 拥有整个树的副本。 但我想在我尝试使用的 python 中做同样的事情:

import copy
def postOrder(self, tnode):
    tcnode = copy.copy(tnode)

但它不起作用。

【问题讨论】:

    标签: python-3.x data-structures


    【解决方案1】:

    使用tcnode=copy.deepcopy(tnode),因为 deepcopy 不会更改原始树,但在您的情况下,您使用的是浅拷贝,在这种情况下它将更改原始树,有关 deepcopyDifference between DeepCopy and Shallow copy 的更多信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-10
      • 2019-12-01
      • 2021-10-30
      • 2014-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多