From 02f32577398dba9960694de27dd85996874038e0 Mon Sep 17 00:00:00 2001 From: Shuvojit Samanta <142198651+shuvojitss@users.noreply.github.com> Date: Wed, 30 Oct 2024 20:07:20 +0530 Subject: [PATCH 1/3] Create program.c --- .../Symmetric Tree/program.c | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Binary Tree Algorithms/Symmetric Tree/program.c diff --git a/Binary Tree Algorithms/Symmetric Tree/program.c b/Binary Tree Algorithms/Symmetric Tree/program.c new file mode 100644 index 00000000..c9bd62b2 --- /dev/null +++ b/Binary Tree Algorithms/Symmetric Tree/program.c @@ -0,0 +1,57 @@ +#include +#include + +struct TreeNode { + int val; + struct TreeNode *left; + struct TreeNode *right; +}; + +// Helper function to check if two trees are mirrors of each other +int isMirror(struct TreeNode* left, struct TreeNode* right) { + if (left == NULL && right == NULL) { + return 1; // Both nodes are NULL, so they are symmetric + } + if (left == NULL || right == NULL) { + return 0; // One of the nodes is NULL, so they are not symmetric + } + // Check if the current nodes have the same value and + // the left subtree of one side is a mirror of the right subtree of the other side + return (left->val == right->val) && + isMirror(left->left, right->right) && + isMirror(left->right, right->left); +} + +// Main function to check if a tree is symmetric +int isSymmetric(struct TreeNode* root) { + if (root == NULL) { + return 1; // An empty tree is symmetric + } + // Use the helper function to compare the left and right subtrees + return isMirror(root->left, root->right); +} + +// Utility function to create a new tree node +struct TreeNode* createNode(int value) { + struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct TreeNode)); + newNode->val = value; + newNode->left = NULL; + newNode->right = NULL; + return newNode; +} + +int main() { + struct TreeNode* root = createNode(1); + root->left = createNode(2); + root->right = createNode(2); + root->left->left = createNode(3); + root->left->right = createNode(4); + root->right->left = createNode(4); + root->right->right = createNode(3); + if (isSymmetric(root)) { + printf("The tree is symmetrical\n"); + } else { + printf("The tree is not symmetrical\n"); + } + return 0; +} From d6340274b57ea2b97c17b7b90a84f4ec96711710 Mon Sep 17 00:00:00 2001 From: Shuvojit Samanta <142198651+shuvojitss@users.noreply.github.com> Date: Wed, 30 Oct 2024 20:18:17 +0530 Subject: [PATCH 2/3] Create README.md --- .../Symmetric Tree/README.md | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Binary Tree Algorithms/Symmetric Tree/README.md diff --git a/Binary Tree Algorithms/Symmetric Tree/README.md b/Binary Tree Algorithms/Symmetric Tree/README.md new file mode 100644 index 00000000..37016cb7 --- /dev/null +++ b/Binary Tree Algorithms/Symmetric Tree/README.md @@ -0,0 +1,45 @@ +### Problem Statement: +Given a Binary Tree, determine whether the given tree is symmetric or not. A Binary Tree would be Symmetric, when its mirror image is exactly the same as the original tree. If we were to draw a vertical line through the centre of the tree, the nodes on the left and right side would be mirror images of each other. + +### Approach: + If the tree is empty, it is symmetric by definition. Otherwise, recursively check if the left subtree is a mirror image of the right subtree by comparing each node's left and right children in opposite order. + +### Algorithm Steps: +1. **Base Case**: + - If the root node is `NULL`, the tree is symmetric by definition. + +2. **Recursive Check**: + - Define a helper function `isMirror(left, right)` that: + - Returns `true` if both `left` and `right` nodes are `NULL` (indicating both subtrees are empty). + - Returns `false` if only one of `left` or `right` is `NULL` (indicating subtrees are not balanced). + - Checks if `left` and `right` node values are equal. + - Recursively compares: + - `left->left` with `right->right` + - `left->right` with `right->left` + +3. **Main Function**: + - Call `isMirror(root->left, root->right)` in the main function to check if the left and right subtrees of the root are mirror images. + +4. **Return Result**: + - If all recursive checks are successful, the tree is symmetric; otherwise, it is not. + +### Sample Input: + 1 + / \ + 2 2 + / \ / \ + 3 4 4 3 + + +### Sample Output: + The tree is symmetrical + +### Explanation of Sample: + In this tree: + 1. The root node (`1`) has two children, both labeled `2`, which match symmetrically. + 2. The left subtree of the left `2` node contains `3` (left) and `4` (right), while the right subtree of the right `2` node contains `4` (left) and `3` (right). + 3. Each pair of corresponding nodes matches in value and structure, confirming that the tree is a mirror image of itself. + Thus, the function confirms symmetry and outputs that the tree is symmetrical. + +### Time Complexity: +- The time complexity is `(O(n)`, where `n` is the number of nodes, as each node is visited once to verify symmetry. From 59918b5d9c93ca8f5c0a272e77216b3f96ef2077 Mon Sep 17 00:00:00 2001 From: Shuvojit Samanta <142198651+shuvojitss@users.noreply.github.com> Date: Wed, 30 Oct 2024 20:19:49 +0530 Subject: [PATCH 3/3] Update README.md --- Binary Tree Algorithms/Symmetric Tree/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Binary Tree Algorithms/Symmetric Tree/README.md b/Binary Tree Algorithms/Symmetric Tree/README.md index 37016cb7..82e14005 100644 --- a/Binary Tree Algorithms/Symmetric Tree/README.md +++ b/Binary Tree Algorithms/Symmetric Tree/README.md @@ -1,3 +1,4 @@ +# Symmetric Tree ### Problem Statement: Given a Binary Tree, determine whether the given tree is symmetric or not. A Binary Tree would be Symmetric, when its mirror image is exactly the same as the original tree. If we were to draw a vertical line through the centre of the tree, the nodes on the left and right side would be mirror images of each other.