forked from Masked-coder11/gfg-POTD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
06.02.2024.cpp
55 lines (46 loc) · 1.04 KB
/
06.02.2024.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
//Node Structure
/*struct Node
{
int k;
Node *left, *right;
};*/
class Solution
{
public:
int check(Node* node, int k, int d){
if(node->left==NULL && node->right==NULL){
if(d==k){
return 1;
}
else{
return 0;
}
}
int c=0;
if(node->left){
c =c | check(node->left,k,d+1);
}
if(node->right){
c= c| check(node->right, k, d+1);
}
return c;
}
int traverse(Node* root, int k){
if(root==NULL) return 0;
int ans=0;
if(check(root,k, 0)){
ans++;
}
ans+=traverse(root->left,k);
ans+=traverse(root->right,k);
return ans;
}
//Function to return count of nodes at a given distance from leaf nodes.
int printKDistantfromLeaf(Node* root, int k)
{
//Add your code here.
int ans=0;
ans+= traverse(root, k);
return ans;
}
};