-
Notifications
You must be signed in to change notification settings - Fork 0
/
singleValueTree.cpp
50 lines (45 loc) · 1.2 KB
/
singleValueTree.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
bool countOptimized_Util(struct node *root, int *counter)
{
if(!root)
return true;
bool l=countOptimized_Util(root->left,counter);
bool r=countOptimized_Util(root->right,counter);
// both left and right subtrees are unival
if(l&&r)
{
struct node *rl=root->left;
struct node *rr=root->right;
// if leaf node
if(!rl && !rr)
{
(*counter)++;
return true;
}
// left and right child exists and their data is also same as root's data
else if(rl && rr && rl->data==root->data && rr->data==root->data)
{
(*counter)++;
return true;
}
// only left child exists and its data is same as root's data
else if(rl && rl->data==root->data)
{
(*counter)++;
return true;
}
// only right child exists and its data is same as root's data
else if(rr && rr->data==root->data)
{
(*counter)++;
return true;
}
}
return false;
}
// Counts the number of unival subtrees
int count(struct node *root)
{
int counter=0;
countSV(root,counter);
return counter;
}