Write the declaration of binary tree as ADT. In this ADT, write a function in to count number of nodes in a binary tree
#include <iostream>
using namespace std;
struct Node {
struct Node * left;
struct Node * right;
};
int countNodes(const struct Node * root)
{
if (!root) return 0;
int left = countNodes(root->left);
int right = countNodes(root->right);
return 1 + left + right;
}
int main()
{
struct Node * root = new Node;
root->left = new Node;
root->right = new Node;
cout << countNodes(root) << endl;
return 0;
}
Comments
Leave a comment