-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBSTcountnodes.cpp
54 lines (45 loc) · 1.07 KB
/
BSTcountnodes.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
#include <bits/stdc++.h>
using namespace std;
// A binary tree Node has data, pointer to left
// child and a pointer to right child
struct Node
{
int data;
struct Node* left, *right;
};
// Function to get the count of full Nodes in
// a binary tree
unsigned int getfullCount(struct Node* root)
{
if (root == NULL)
return 0;
int res = 0;
if (root->left && root->right)
res++;
res += (getfullCount(root->left) +
getfullCount(root->right));
return res;
}
/* Helper function that allocates a new
Node with the given data and NULL left
and right pointers. */
struct Node* newNode(int data)
{
struct Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return (node);
}
int main(void)
{
struct Node *root = newNode(2);
root->left = newNode(7);
root->right = newNode(5);
root->left->right = newNode(6);
root->left->right->left = newNode(1);
root->left->right->right = newNode(11);
root->right->right = newNode(9);
root->right->right->left = newNode(4);
cout << getfullCount(root);
return 0;
}