-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSymmetric-Tree.cpp
52 lines (46 loc) · 1.25 KB
/
Symmetric-Tree.cpp
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
44
45
46
47
48
49
50
51
52
#include <bits/stdc++.h>
using namespace std;
template <typename T>
class BinaryTreeNode {
public :
T data;
BinaryTreeNode<T> *left;
BinaryTreeNode<T> *right;
BinaryTreeNode(T data) {
this -> data = data;
left = NULL;
right = NULL;
}
~BinaryTreeNode() {
if(left)
delete left;
if(right)
delete right;
}
};
// Helper function to check if two trees are mirror of each other
bool helper(BinaryTreeNode<int> *left, BinaryTreeNode<int> *right) {
// If both are null, they are mirror of each other
if(!left && !right) {
return true;
}
// If one of them is null, they are not mirror of each other
if(!left ^ !right) {
return false;
}
// If both are not null, check if their data is same
// and their left and right are mirror of each other
if(left->data != right->data) {
return false;
}
return helper(left->left, right->right) && helper(left->right, right->left);
}
bool isSymmetric(BinaryTreeNode<int>* root)
{
// If root is null, it is symmetric
if(!root) {
return true;
}
// Else check if left and right subtree are mirror of each other
return helper(root->left, root->right);
}