From c0088aba3e946fdd98a2112c09a9dfae1b138250 Mon Sep 17 00:00:00 2001 From: AgathaBahati Date: Fri, 4 Oct 2024 13:42:37 +0300 Subject: [PATCH 1/3] feat: add symmetric tree --- binary_tree/symmetric_tree/__init__.py | 0 binary_tree/symmetric_tree/symmetric_tree.py | 23 ++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 binary_tree/symmetric_tree/__init__.py create mode 100644 binary_tree/symmetric_tree/symmetric_tree.py diff --git a/binary_tree/symmetric_tree/__init__.py b/binary_tree/symmetric_tree/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/binary_tree/symmetric_tree/symmetric_tree.py b/binary_tree/symmetric_tree/symmetric_tree.py new file mode 100644 index 0000000..14f5b78 --- /dev/null +++ b/binary_tree/symmetric_tree/symmetric_tree.py @@ -0,0 +1,23 @@ +from collections import deque +def symmetric_tree(root): + queue = deque([root.left, root.right]) + + while queue: + curr_left = queue.popleft() + curr_right = queue.popleft() + + if not curr_left and not curr_right: + continue + + if not curr_right or not curr_left: + return False + + if curr_left.val != curr_right.val: + return False + + queue.append(curr_left.left) + queue.append(curr_right.right) + queue.append(curr_left.right) + queue.append(curr_right.left) + + return True \ No newline at end of file From c6ad40ec5ec24086f7d413680d393fc0e58897c1 Mon Sep 17 00:00:00 2001 From: AgathaBahati Date: Fri, 4 Oct 2024 13:45:23 +0300 Subject: [PATCH 2/3] doc:add comments --- binary_tree/symmetric_tree/symmetric_tree.py | 42 +++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/binary_tree/symmetric_tree/symmetric_tree.py b/binary_tree/symmetric_tree/symmetric_tree.py index 14f5b78..934bb2b 100644 --- a/binary_tree/symmetric_tree/symmetric_tree.py +++ b/binary_tree/symmetric_tree/symmetric_tree.py @@ -1,23 +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 \ No newline at end of file + 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. \ No newline at end of file From e0cd8b61a75ca1e1abafbe810a152b514c55d795 Mon Sep 17 00:00:00 2001 From: AgathaBahati Date: Fri, 4 Oct 2024 13:47:45 +0300 Subject: [PATCH 3/3] doc: add README --- binary_tree/symmetric_tree/README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 binary_tree/symmetric_tree/README.md diff --git a/binary_tree/symmetric_tree/README.md b/binary_tree/symmetric_tree/README.md new file mode 100644 index 0000000..f63a6f4 --- /dev/null +++ b/binary_tree/symmetric_tree/README.md @@ -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 \ No newline at end of file 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