Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
MoigeMatino merged 3 commits intomainfromadd-symmetric-tree
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionsbinary_tree/symmetric_tree/README.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Empty file.
63 changes: 63 additions & 0 deletionsbinary_tree/symmetric_tree/symmetric_tree.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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.

[8]ページ先頭

©2009-2025 Movatter.jp