-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaily8_easy_google.hpp
62 lines (47 loc) · 1.35 KB
/
daily8_easy_google.hpp
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
/*
his problem was asked by Google.
A unival tree (which stands for "universal value") is a tree where all nodes under it have the same value.
Given the root to a binary tree, count the number of unival subtrees.
For example, the following tree has 5 unival subtrees:
0
/ \
1 0
/ \
1 0
/ \
1 1
*/
#include <iostream>
#include <string>
struct Node{
int val;
Node* left;
Node* right;
};
int findUnivalCount(Node *root){
if(root==nullptr){
return 0;
}
int res = 0;
if(root->left == nullptr && root->right==nullptr){
res = 1;
}else if(root->left != nullptr && root->right !=nullptr && root->left->val == root->right->val){
res = 1;
}
res += findUnivalCount(root->left)+findUnivalCount(root->right);
return res;
}
int main() {
Node tree1 = {.val = 1, .left=nullptr, .right=nullptr};
std::cout<<findUnivalCount(&tree1)<<std::endl;
Node tree2 = {.val = 1, .left=new Node{1, nullptr, nullptr}, .right=nullptr};
std::cout<<findUnivalCount(&tree2)<<std::endl;
Node tree3 = {0, new Node{1,nullptr, nullptr},
new Node{0,
new Node{1,
new Node{1, nullptr, nullptr},
new Node{1, nullptr, nullptr}},
new Node{0,nullptr, nullptr}}};
std::cout<<findUnivalCount(&tree3)<<std::endl;
return 0;
}