mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Update bt_is_tree_symmetric.py
This commit is contained in:
parent
153e85ca03
commit
f41011e849
@ -3,7 +3,7 @@
|
|||||||
# author: bt3gl
|
# author: bt3gl
|
||||||
|
|
||||||
|
|
||||||
def is_symmetric(root: Optional[Node]) -> bool:
|
def is_symmetric(root) -> bool:
|
||||||
|
|
||||||
stack = [(root, root)]
|
stack = [(root, root)]
|
||||||
|
|
||||||
@ -14,19 +14,19 @@ def is_symmetric(root: Optional[Node]) -> bool:
|
|||||||
if (not node1 and node2) or (not node2 and node1):
|
if (not node1 and node2) or (not node2 and node1):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
elif not node1 and not node2:
|
|
||||||
continue
|
|
||||||
|
|
||||||
elif node1 and node2 and node1.val != node2.val:
|
elif node1 and node2 and node1.val != node2.val:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
elif not node1 and not node2:
|
||||||
|
continue
|
||||||
|
|
||||||
stack.append([node1.left, node2.right])
|
stack.append([node1.left, node2.right])
|
||||||
stack.append([node1.right, node2.left])
|
stack.append([node1.right, node2.left])
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def is_symmetric_recursive(root: Optional[TreeNode]) -> bool:
|
def is_symmetric_recursive(root) -> bool:
|
||||||
|
|
||||||
def helper(node1, node2):
|
def helper(node1, node2):
|
||||||
if (not node1 and node2) or \
|
if (not node1 and node2) or \
|
||||||
|
Loading…
x
Reference in New Issue
Block a user