Skip to content

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 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 3 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 10 additions & 0 deletions binary_tree/symmetric_tree/README.md
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.
63 changes: 63 additions & 0 deletions binary_tree/symmetric_tree/symmetric_tree.py
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.
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy