MCPcopy Create free account
hub / github.com/joowani/binarytree / clone

Method clone

binarytree/__init__.py:776–801  ·  view source on GitHub ↗

Return a clone of this binary tree. :return: Root of the clone. :rtype: binarytree.Node

(self)

Source from the content-addressed store, hash-verified

774 return True
775
776 def clone(self) -> "Node":
777 """Return a clone of this binary tree.
778
779 :return: Root of the clone.
780 :rtype: binarytree.Node
781 """
782 other = Node(self.val)
783
784 stack1 = [self]
785 stack2 = [other]
786
787 while stack1 or stack2:
788 node1 = stack1.pop()
789 node2 = stack2.pop()
790
791 if node1.left is not None:
792 node2.left = Node(node1.left.val)
793 stack1.append(node1.left)
794 stack2.append(node2.left)
795
796 if node1.right is not None:
797 node2.right = Node(node1.right.val)
798 stack1.append(node1.right)
799 stack2.append(node2.right)
800
801 return other
802
803 @property
804 def values(self) -> List[Optional[NodeValue]]:

Callers 2

Calls 1

NodeClass · 0.85

Tested by 2