-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMirror.cpp
More file actions
31 lines (31 loc) · 830 Bytes
/
Mirror.cpp
File metadata and controls
31 lines (31 loc) · 830 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
#include <iostream>
using namespace std;
struct Node {
int val;
Node* left;
Node* right;
Node(int x) : val(x), left(NULL), right(NULL) {}
};
bool isMirror(Node* a, Node* b) {
if (!a && !b) return true;
if (!a || !b) return false;
return (a->val == b->val) &&
isMirror(a->left, b->right) &&
isMirror(a->right, b->left);
}
bool isSymmetric(Node* root) {
if (!root) return true;
return isMirror(root->left, root->right);
}
int main() {
// Example: [1,2,2,3,4,4,3]
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(2);
root->left->left = new Node(3);
root->left->right = new Node(4);
root->right->left = new Node(4);
root->right->right = new Node(3);
cout << (isSymmetric(root) ? "true" : "false");
return 0;
}