-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinternalExternal.cpp
More file actions
53 lines (43 loc) · 988 Bytes
/
internalExternal.cpp
File metadata and controls
53 lines (43 loc) · 988 Bytes
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 <iostream>
using namespace std;
struct Node{
int data;
struct Node *left;
struct Node *right;
};
int leafCount(struct Node *root){
if(root == NULL)
return 0;
else if(root->left == NULL && root->right == NULL)
return 1;
else
return leafCount(root->left) + leafCount(root->right);
}
int totalCount(struct Node *root){
if(root == NULL)
return 0;
else if(root->left == NULL && root->right == NULL)
return 1;
else
return 1 + totalCount(root->right) + totalCount(root->left);
};
struct Node *getNode(int data){
struct Node *tmp = new Node();
tmp ->data = data;
tmp ->left = NULL;
tmp -> right = NULL;
return tmp;
}
int main() {
struct Node *root = getNode(1);
root->left = getNode(2);
root->right = getNode(3);
root->left->left = getNode(4);
root->left->right = getNode(5);
int l, t;
l = leafCount(root);
t = totalCount(root);
cout<<"Number of leaf nodes :"<<l<<"\n";
cout<<"Number of internal nodes :"<<t - l;
return 0;
}