forked from Harshita-Kanal/Data-Structures-and-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcheck_mirror.cpp
More file actions
50 lines (41 loc) · 896 Bytes
/
Copy pathcheck_mirror.cpp
File metadata and controls
50 lines (41 loc) · 896 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
44
45
46
47
48
49
50
// C++ program to check if two n-ary trees are
// mirror.
#include <bits/stdc++.h>
using namespace std;
// Function to check given two trees are mirror
// of each other or not
int checkMirrorTree(int M, int N, int u1[ ],
int v1[ ] , int u2[], int v2[])
{
// Map to store nodes of the tree
unordered_map<int , stack<int>>mp;
// Traverse first tree nodes
for (int i = 0 ; i < N ; i++ )
{
mp[u1[i]].push(v1[i]);
}
// Traverse second tree nodes
for (int i = 0 ; i < N ; i++)
{
if(mp[u2[i]].top() != v2[i])
return 0;
mp[u2[i]].pop();
}
return 1;
}
// Driver code
int main()
{
int M = 7, N = 6;
//Tree 1
int u1[] = { 1, 1, 1, 10, 10, 10 };
int v1[] = { 10, 7, 3, 4, 5, 6 };
//Tree 2
int u2[] = { 1, 1, 1, 10, 10, 10 };
int v2[] = { 3, 7, 10, 6, 5, 4 };
if(checkMirrorTree(M, N, u1, v1, u2, v2))
cout<<"Yes";
else
cout<<"No";
return 0;
}