-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathis_a_Binary_Tree_Symmetrical_.cpp
More file actions
43 lines (35 loc) · 979 Bytes
/
Copy pathis_a_Binary_Tree_Symmetrical_.cpp
File metadata and controls
43 lines (35 loc) · 979 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <malloc.h>
#include <limits.h>
/* nodes for queue and tree node*/
struct node {
int key;
struct node * left;
struct node * right;
};
/* function for creating new node of tree*/
struct node *newNode(int data)
{
struct node *node = (struct node *)malloc(sizeof(struct node));
node->key = data;
node->left = NULL;
node->right = NULL;
return (node);
}
/* function to determine the diameter of a binary tree */
int isSymmetrical(struct node * x, struct node * y){
if(x == NULL && y == NULL)return 1;
return (x != NULL && y != NULL) &&
isSymmetrical(x->left, y->right) &&
isSymmetrical(x->right, y->left);
}
int main(){
struct node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->right = newNode(4);
root->right->left = newNode(5);
if(isSymmetrical(root->left,root->right)) printf("YES");
else printf("NO");
return 0;
}