-
Notifications
You must be signed in to change notification settings - Fork 0
/
Count-Leave.cpp
43 lines (32 loc) · 917 Bytes
/
Count-Leave.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
/**********************************************************
Following is the Binary Tree Node class structure:
template <typename T>
class BinaryTreeNode {
public :
T data;
BinaryTreeNode<T> *left;
BinaryTreeNode<T> *right;
BinaryTreeNode(T data) {
this -> data = data;
left = NULL;
right = NULL;
}
};
***********************************************************/
void inorder(BinaryTreeNode<int> * root, int &count) {
//base case
if(root == NULL) {
return ;
}
inorder(root->left, count);
//leaf node
if(root->left == NULL && root->right == NULL) {
count++;
}
inorder(root->right, count);
}
int noOfLeafNodes(BinaryTreeNode<int> *root){
int cnt = 0;
inorder(root, cnt);
return cnt;
}