-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#08.cpp
60 lines (50 loc) · 1.65 KB
/
#08.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
#include <iostream>
using namespace std;
class TreeNode {
public:
int value;
TreeNode* left;
TreeNode* right;
TreeNode(int val) {
value = val;
left = nullptr;
right = nullptr;
}
};
// At each node must return whether tree under node is unival
// and the number of unival trees under
pair<bool, int> getUnivalTrees(TreeNode* t) {
if (t->left == nullptr && t->right == nullptr) {
return make_pair(true, 1);
} else if (t->left == nullptr) {
pair<bool, int> p = getUnivalTrees(t->right);
// Check if current tree is unival
if (p.first && t->value == t->right->value) return make_pair(true, p.second + 1);
else return make_pair(false, p.second);
} else if (t->right == nullptr) {
pair<bool, int> p = getUnivalTrees(t->left);
// Check if current tree is unival
if (p.first && t->value == t->left->value) return make_pair(true, p.second + 1);
else return make_pair(false, p.second);
} else {
pair<bool, int> left = getUnivalTrees(t->left);
pair<bool, int> right = getUnivalTrees(t->right);
if (left.first && right.first && t->value == t->right->value && t->value == t->left->value) {
// Both branches and current tree are all unival
return make_pair(true, left.second + right.second + 1);
} else {
// Sum number of unival subtrees from both branches
return make_pair(false, left.second + right.second);
}
}
}
int main() {
TreeNode* root = new TreeNode(0);
root->left = new TreeNode(1);
root->right = new TreeNode(0);
root->right->left = new TreeNode(1);
root->right->right = new TreeNode(0);
root->right->left->left = new TreeNode(1);
root->right->left->right = new TreeNode(1);
cout << getUnivalTrees(root).second << endl;
}