Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
9c90ab7
symmectric tree
JeevaRamanathan Oct 5, 2023
632b1d3
Merge branch 'TheAlgorithms:master' into symmetric_tree
JeevaRamanathan Oct 5, 2023
f6b261e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
fa42254
removed trailing spaces
JeevaRamanathan Oct 5, 2023
90d8c41
removed trailing spaces
JeevaRamanathan Oct 5, 2023
f7ea89c
escape sequence fix
JeevaRamanathan Oct 5, 2023
6cd5628
added return type
JeevaRamanathan Oct 5, 2023
7c8afa6
added class
JeevaRamanathan Oct 5, 2023
4d16aeb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
e305ae3
wordings fix
JeevaRamanathan Oct 5, 2023
1e73db3
wordings fix
JeevaRamanathan Oct 5, 2023
1964155
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
621959d
added static method
JeevaRamanathan Oct 5, 2023
1aec628
Merge branch 'symmetric_tree' of https://github.com/JeevaRamanathan/P…
JeevaRamanathan Oct 5, 2023
44d843a
added type
JeevaRamanathan Oct 5, 2023
0b4c1f2
added static method
JeevaRamanathan Oct 5, 2023
4dafe7e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
222cd70
wordings fix
JeevaRamanathan Oct 5, 2023
662fa97
wordings fix
JeevaRamanathan Oct 5, 2023
cf24dce
testcase added
JeevaRamanathan Oct 5, 2023
f98f199
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
1229d85
testcase added for mirror function
JeevaRamanathan Oct 5, 2023
8542cff
Merge branch 'symmetric_tree' of https://github.com/JeevaRamanathan/P…
JeevaRamanathan Oct 5, 2023
6bfe7c7
testcase added for mirror function
JeevaRamanathan Oct 5, 2023
6d53919
made the requested changes
JeevaRamanathan Oct 5, 2023
9bd84a9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
1d43d8c
made the requested changes
JeevaRamanathan Oct 5, 2023
ef0bbab
Merge branch 'symmetric_tree' of https://github.com/JeevaRamanathan/P…
JeevaRamanathan Oct 5, 2023
4fa0769
doc test added for symmetric, asymmetric
JeevaRamanathan Oct 5, 2023
62b4a80
Update symmetric_tree.py
cclauss Oct 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
testcase added
  • Loading branch information
JeevaRamanathan committed Oct 5, 2023
commit cf24dce3c0f64cd4459928537603f82d5695ffd5
18 changes: 13 additions & 5 deletions data_structures/binary_tree/symmetric_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,17 @@ def is_symmetric_tree(self, tree: Node) -> bool:
return True
return self.is_mirror(tree.left, tree.right)

@staticmethod
def is_mirror(left: Node | None, right: Node | None) -> bool:
def is_mirror(self, left: Node | None, right:Node | Node) -> bool:
"""
Check if two binary trees are mirrors of each other.

Args:
left (Node | None): The root of the left subtree.
right (Node | None): The root of the right subtree.

Returns:
bool: True if the two trees are mirrors, False otherwise.
"""
if left is None and right is None:
# Both sides are empty, which is symmetric.
return True
Expand All @@ -73,9 +82,8 @@ def is_mirror(left: Node | None, right: Node | None) -> bool:
return False
if left.data == right.data:
# The values match, so check the subtree
return SymmetricTree.is_mirror(
left.left, right.right
) and SymmetricTree.is_mirror(left.right, right.left)
return self.is_mirror(left.left, right.right) \
and self.is_mirror(left.right, right.left)
return False


Expand Down