-
Notifications
You must be signed in to change notification settings - Fork 0
/
dailycoding008.cpp
89 lines (71 loc) · 1.89 KB
/
dailycoding008.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
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
#include <iostream>
using std::cout;
using std::endl;
struct Node {
int val;
Node *left, *right;
};
Node* newNode(int val) {
Node *temp = new Node;
temp->val = val;
temp->left = temp->right = NULL;
return temp;
}
bool isUnival(Node *root, int &count) {
if (root == NULL) return true;
bool left = isUnival(root->left, count);
bool right = isUnival(root->right, count);
if (!left || !right) return false;
if (root->left && root->val != root->left->val) {
return false;
}
if (root->right && root->val != root->right->val) {
return false;
}
count++;
return true;
}
int countUnivals(Node *root) {
int count = 0; isUnival(root, count);
return count;
}
int main() {
Node *root1 = newNode(0);
root1->left = newNode(1);
root1->right = newNode(0);
root1->right->left = newNode(1);
root1->right->right = newNode(0);
root1->right->left->left = newNode(1);
root1->right->left->right = newNode(1);
Node *root2 = newNode(0);
Node *temp = root2;
for (int i = 0; i < 3; i++) {
temp->right = newNode(0);
temp = temp->right;
}
Node *root3 = newNode(0);
root3->right = newNode(0);
root3->right = newNode(1);
Node *root4 = newNode(1);
root4->left = newNode(1);
root4->right = newNode(1);
Node *root5 = newNode(1);
root5->left = newNode(1);
root5->right = newNode(1);
root5->right->right = newNode(0);
Node *root6 = newNode(0);
root6->left = newNode(1);
root6->left->left = newNode(1);
root6->left->right = newNode(1);
if (countUnivals(root1) == 5 &&
countUnivals(root2) == 4 &&
countUnivals(root3) == 1 &&
countUnivals(root4) == 3 &&
countUnivals(root5) == 2 &&
countUnivals(root6) == 3) {
cout << "Passed" << endl;
} else {
cout << "Failed" << endl;
}
return 0;
}