Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Symmetric tree #9871

Merged
merged 30 commits into from
Oct 6, 2023
Merged
Changes from 14 commits
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
85 changes: 85 additions & 0 deletions data_structures/binary_tree/symmetric_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"""
Given the root of a binary tree, check whether it is a mirror of itself
(i.e., symmetric around its center).

Leetcode reference: https://leetcode.com/problems/symmetric-tree/
"""


class Node:
"""
A Node has data variable and pointers to Nodes to its left and right.
"""

def __init__(self, data: int) -> None:
self.data = data
self.left: Node | None = None
self.right: Node | None = None


class SymmetricTree:
def is_symmetric_tree(self, tree: Node) -> bool:
r"""
Test cases for is_symmetric_tree function.

The below tree looks like this
1
/ \
2 2
/ \ / \
3 4 4 3

>>> tree = Node(1)
>>> tree.left = Node(2)
>>> tree.right = Node(2)
>>> tree.left.left = Node(3)
>>> tree.left.right = Node(4)
>>> tree.right.left = Node(4)
>>> tree.right.right = Node(3)

>>> SymmetricTree().is_symmetric_tree(tree)
True

The below tree looks like this
1
/ \
2 2
/ \ / \
3 4 3 4

>>> tree2 = Node(1)
>>> tree2.left = Node(2)
>>> tree2.right = Node(2)
>>> tree2.left.left = Node(3)
>>> tree2.left.right = Node(4)
>>> tree2.right.left = Node(3)
>>> tree2.right.right = Node(4)

>>> SymmetricTree().is_symmetric_tree(tree2)
False
"""
if tree is None:
# An empty tree is considered symmetric.
return True
return self.is_mirror(tree.left, tree.right)

@staticmethod
def is_mirror(self, left, right) -> bool:
if left is None and right is None:
# Both sides are empty, which is symmetric.
return True
if left is None or right is None:
# One side is empty while the other is not, which is not symmetric.
return False
if left.data == right.data:
# The values match, so check the subtree
return self.is_mirror(left.left, right.right) and self.is_mirror(
left.right, right.left
)
return False


if __name__ == "__main__":
from doctest import testmod

testmod()