-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathis_symmetric.cpp
More file actions
118 lines (94 loc) · 2.6 KB
/
is_symmetric.cpp
File metadata and controls
118 lines (94 loc) · 2.6 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
// Created by Mayank Parasar on 2020-01-30.
//
/*
* A k-ary tree is a tree with k-children, and a tree is symmetrical
* if the data of the left side of the tree is the same as the right side of the tree.
Here's an example of a symmetrical k-ary tree.
4
/ \
3 3
/ | \ / | \
9 4 1 1 4 9
Given a k-ary tree, figure out if the tree is symmetrical.
*
*/
#include <iostream>
#include <vector>
using namespace std;
struct node {
int val;
vector<node*> child;
node(int x) : val(x)
{}
};
//
bool is_equal(node* left, node* right) { // this is the recursive function
// base condition goes here...
if(left == nullptr && right == nullptr) {
return true;
}
else if(left != nullptr && right == nullptr) {
return false;
}
else if(left == nullptr && right != nullptr) {
return false;
}
else if( left->child.size() != right->child.size()) {
return false;
}
// traverse the children of left from idx 0
// traverse the children of right from idx size-1
// recursively call this function
int start, end;
for(start=0, end = (right->child.size() - 1); ((end >= 0) && (start <= left->child.size() - 1));
end--, start++) {
is_equal(left->child[start], right->child[end]);
}
// base condition geos here...
if(left->val == right->val) {
return true;
}
else if(left->val != right->val) {
return false;
}
}
bool is_symmetric(node* node_) { // this is the driver function...
// do a breath first search on child
// traverse the vector from both sides;
int start, end;
for(start = 0, end = (node_->child.size() - 1);
end > start; start++, end--) {
if(is_equal(node_->child[start], node_->child[end])) {
continue;
}
else {
return false;
}
}
// all the children of current node exhausted...
// go to next
return true;
}
int main() {
node *node1 = new node(4);
node *node2 = new node(3);
node *node3 = new node(3);
node *node4 = new node(9);
node *node5 = new node(4);
node *node6 = new node(1);
node *node7 = new node(1);
node *node8 = new node(4);
node *node9 = new node(9);
node1->child.push_back(node2);
node1->child.push_back(node3);
node2->child.push_back(node4);
node2->child.push_back(node5);
node2->child.push_back(node6);
node3->child.push_back(node7);
node3->child.push_back(node8);
node3->child.push_back(node9);
cout << boolalpha;
cout << is_symmetric(node1);
return 0;
}