- Notifications
You must be signed in to change notification settings - Fork12
feat: add symmetric tree#362
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
3 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
10 changes: 10 additions & 0 deletionsbinary_tree/symmetric_tree/README.md
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,10 @@ | ||
## **Problem Statement** | ||
Given the root of a binary tree, check whether it is a symmetric tree. A symmetric tree refers to a tree that is the mirror of itself, i.e., symmetric around its root. | ||
### Constraints | ||
> 1 ≤ Number of nodes in the tree ≤ 500. | ||
> −1000 ≤ Node.value ≤ 1000 |
Empty file addedbinary_tree/symmetric_tree/__init__.py
Empty file.
63 changes: 63 additions & 0 deletionsbinary_tree/symmetric_tree/symmetric_tree.py
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,63 @@ | ||
from collections import deque | ||
def symmetric_tree(root): | ||
""" | ||
Determine if a binary tree is symmetric around its center. | ||
Parameters: | ||
root (Node): The root node of the binary tree. | ||
Returns: | ||
bool: True if the tree is symmetric, False otherwise. | ||
""" | ||
queue = deque([root.left, root.right]) | ||
while queue: | ||
curr_left = queue.popleft() | ||
curr_right = queue.popleft() | ||
# If both nodes are None, they are symmetric; continue to the next pair | ||
if not curr_left and not curr_right: | ||
continue | ||
# If only one of the nodes is None, the tree is not symmetric | ||
if not curr_right or not curr_left: | ||
return False | ||
# If the values of the nodes are not equal, the tree is not symmetric | ||
if curr_left.val != curr_right.val: | ||
return False | ||
# Append children in mirrored order to maintain symmetry check | ||
queue.append(curr_left.left) | ||
queue.append(curr_right.right) | ||
queue.append(curr_left.right) | ||
queue.append(curr_right.left) | ||
return True | ||
# Approach: | ||
# --------- | ||
# The function uses an iterative approach with a queue to perform a level-order traversal, | ||
# comparing nodes in pairs to check for symmetry. The idea is to compare the left and right | ||
# subtrees of the tree simultaneously, ensuring that they mirror each other. | ||
# Steps: | ||
# 1. Initialize a queue with the left and right children of the root. | ||
# 2. While the queue is not empty, pop two nodes at a time (curr_left and curr_right). | ||
# 3. If both nodes are None, continue to the next pair (they are symmetric). | ||
# 4. If only one of the nodes is None, return False (they are not symmetric). | ||
# 5. If the values of the nodes are not equal, return False (they are not symmetric). | ||
# 6. Append the children of curr_left and curr_right to the queue in a mirrored order: | ||
# - Append curr_left.left and curr_right.right | ||
# - Append curr_left.right and curr_right.left | ||
# 7. If all pairs are symmetric, return True. | ||
# Time Complexity: | ||
# ---------------- | ||
# O(n), where n is the number of nodes in the tree. Each node is enqueued and dequeued once. | ||
# Space Complexity: | ||
# ----------------- | ||
# O(n), where n is the number of nodes in the tree. In the worst case, the space used by the queue | ||
# is proportional to the number of nodes at the widest level of the tree, which can be up to n/2. |
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.