MCPcopy
hub / github.com/joowani/binarytree / equals

Method equals

binarytree/__init__.py:745–774  ·  view source on GitHub ↗

Check if this binary tree is equal to other binary tree. :param other: Root of the other binary tree. :type other: binarytree.Node :return: True if the binary trees are equal, False otherwise. :rtype: bool

(self, other: "Node")

Source from the content-addressed store, hash-verified

743 current_nodes = next_nodes
744
745 def equals(self, other: "Node") -> bool:
746 """Check if this binary tree is equal to other binary tree.
747
748 :param other: Root of the other binary tree.
749 :type other: binarytree.Node
750 :return: True if the binary trees are equal, False otherwise.
751 :rtype: bool
752 """
753 stack1: List[Optional[Node]] = [self]
754 stack2: List[Optional[Node]] = [other]
755
756 while stack1 or stack2:
757 node1 = stack1.pop()
758 node2 = stack2.pop()
759
760 if node1 is None and node2 is None:
761 continue
762 elif node1 is None or node2 is None:
763 return False
764 elif not isinstance(node2, Node):
765 return False
766 else:
767 if node1.val != node2.val:
768 return False
769 stack1.append(node1.right)
770 stack1.append(node1.left)
771 stack2.append(node2.right)
772 stack2.append(node2.left)
773
774 return True
775
776 def clone(self) -> "Node":
777 """Return a clone of this binary tree.

Calls

no outgoing calls