Answer to Question #210649 in C++ for Usman Ali

Question #210649

Write the declaration of binary tree as ADT. In this ADT, write a function in to count number of nodes in a binary tree


1
Expert's answer
2021-06-25T11:34:47-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS