-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1511 from shuvojitss/merge-1
Added the program of Symmetric Tree
- Loading branch information
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,46 @@ | ||
# 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. | ||
|
||
### 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. |
This file contains 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,57 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
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; | ||
} |