forked from shubham-kumar50/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_8.cpp
More file actions
53 lines (49 loc) · 1.3 KB
/
Problem_8.cpp
File metadata and controls
53 lines (49 loc) · 1.3 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
#include<bits/stdc++.h>
using namespace std;
struct node{
long long data;
node* left;
node* right;
};
node* newNode(long long data)
{
node* temp = new node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
};
long long univalTree(node* root , long long count){
if(root == NULL){
return count;
}
else if(root->left == NULL){
if((root->left == NULL and root->right == NULL) or ((root->right)->data == root->data)){
count += 1;
}
}
else if(root->right == NULL){
if((root->left == NULL and root->right == NULL) or ((root->left)->data == root->data)){
count += 1;
}
}
else if(root->left != NULL and root->right != NULL){
if((root->left == NULL and root->right == NULL) or ((root->left)->data == root->data and (root->right)->data == root->data)){
count += 1;
}
}
count = univalTree(root->left , count);
count = univalTree(root->right , count);
}
int main()
{
node* root = NULL;
root = newNode(0);
root->left = newNode(1);
root->right = newNode(0);
root->left->left = newNode(1);
root->right->right = newNode(0);
root->left->right = newNode(1);
long long count = 0;
count = univalTree(root , count);
cout<<count<<endl;
}