-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Symmetric tree #9871
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
Merged
Merged
Symmetric tree #9871
Changes from 28 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
9c90ab7
symmectric tree
JeevaRamanathan 632b1d3
Merge branch 'TheAlgorithms:master' into symmetric_tree
JeevaRamanathan f6b261e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] fa42254
removed trailing spaces
JeevaRamanathan 90d8c41
removed trailing spaces
JeevaRamanathan f7ea89c
escape sequence fix
JeevaRamanathan 6cd5628
added return type
JeevaRamanathan 7c8afa6
added class
JeevaRamanathan 4d16aeb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e305ae3
wordings fix
JeevaRamanathan 1e73db3
wordings fix
JeevaRamanathan 1964155
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 621959d
added static method
JeevaRamanathan 1aec628
Merge branch 'symmetric_tree' of https://github.com/JeevaRamanathan/P…
JeevaRamanathan 44d843a
added type
JeevaRamanathan 0b4c1f2
added static method
JeevaRamanathan 4dafe7e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 222cd70
wordings fix
JeevaRamanathan 662fa97
wordings fix
JeevaRamanathan cf24dce
testcase added
JeevaRamanathan f98f199
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1229d85
testcase added for mirror function
JeevaRamanathan 8542cff
Merge branch 'symmetric_tree' of https://github.com/JeevaRamanathan/P…
JeevaRamanathan 6bfe7c7
testcase added for mirror function
JeevaRamanathan 6d53919
made the requested changes
JeevaRamanathan 9bd84a9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1d43d8c
made the requested changes
JeevaRamanathan ef0bbab
Merge branch 'symmetric_tree' of https://github.com/JeevaRamanathan/P…
JeevaRamanathan 4fa0769
doc test added for symmetric, asymmetric
JeevaRamanathan 62b4a80
Update symmetric_tree.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
""" | ||
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/ | ||
""" | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class Node: | ||
""" | ||
A Node has data variable and pointers to Nodes to its left and right. | ||
""" | ||
|
||
data: int | ||
left: Node | None = None | ||
right: Node | None = None | ||
|
||
|
||
def make_symmetric_tree() -> Node: | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
r""" | ||
Create a symmetric tree for testing. | ||
The 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) | ||
return tree | ||
|
||
|
||
def make_asymmetric_tree() -> Node: | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
r""" | ||
Create a asymmetric tree for testing. | ||
The tree looks like this: | ||
1 | ||
/ \ | ||
2 2 | ||
/ \ / \ | ||
3 4 3 4 | ||
""" | ||
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(3) | ||
tree.right.right = Node(4) | ||
return tree | ||
|
||
|
||
class SymmetricTree: | ||
def is_symmetric_tree(self, tree: Node) -> bool: | ||
r""" | ||
Test cases for is_symmetric_tree function | ||
|
||
>>> SymmetricTree().is_symmetric_tree(make_symmetric_tree()) | ||
True | ||
|
||
>>> SymmetricTree().is_symmetric_tree(make_asymmetric_tree()) | ||
False | ||
""" | ||
if tree is None: | ||
# An empty tree is considered symmetric. | ||
return True | ||
return self.is_mirror(tree.left, tree.right) | ||
|
||
def is_mirror(self, left: Node | None, right: Node | None) -> bool: | ||
""" | ||
>>> tree1 = make_symmetric_tree() | ||
>>> tree1.right.right = Node(3) | ||
>>> SymmetricTree().is_mirror(tree1.left, tree1.right) | ||
True | ||
|
||
>>> tree2 = make_asymmetric_tree() | ||
>>> SymmetricTree().is_mirror(tree2.left, tree2.right) | ||
False | ||
""" | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.