MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / is_symmetric_tree

Function is_symmetric_tree

data_structures/binary_tree/symmetric_tree.py:104–122  ·  view source on GitHub ↗

Check if a binary tree is symmetric (i.e., a mirror of itself). Parameters: tree: The root node of the binary tree. Returns: bool: True if the tree is symmetric, False otherwise. Example: >>> is_symmetric_tree(make_symmetric_tree()) True >>> is_symmetric_tree(

(tree: Node)

Source from the content-addressed store, hash-verified

102
103
104def is_symmetric_tree(tree: Node) -> bool:
105 """
106 Check if a binary tree is symmetric (i.e., a mirror of itself).
107
108 Parameters:
109 tree: The root node of the binary tree.
110
111 Returns:
112 bool: True if the tree is symmetric, False otherwise.
113
114 Example:
115 >>> is_symmetric_tree(make_symmetric_tree())
116 True
117 >>> is_symmetric_tree(make_asymmetric_tree())
118 False
119 """
120 if tree:
121 return is_mirror(tree.left, tree.right)
122 return True # An empty tree is considered symmetric.
123
124
125def is_mirror(left: Node | None, right: Node | None) -> bool:

Callers

nothing calls this directly

Calls 1

is_mirrorFunction · 0.85

Tested by

no test coverage detected