Question

In: Computer Science

Write a solution to return the count of the number of nodes in a binary tree....

Write a solution to return the count of the number of nodes in a binary tree. Your method will be passed one parameter, a copy of a pointer to the root node of the tree (Node *) and will return an int that is the count of nodes. If the tree is empty, return 0 (this is the recursive base case).

Solutions

Expert Solution

ANS:

//The number of nodes in Binary tree will be the sum of number of nodes of left subtree plus the number of nodes in //right subtree

int count_recursive(Node *root)

{

    int count = 0;

    if (root->left == NULL && root->right == NULL)

{

        count = 0; // if empty it will return 0

    }

    else

{

        if (root->left != NULL)

{

            count += count_recursive(root->left); //Recursive Call for left subtree

        }

        if (root->right != NULL)

{

            count += count_recursive(root->right); //Recursive call for right subtree

        }

    }

    return count;

}

Comment down for any queries
Please give a thumbs up if you are satisfied with answer :)


Related Solutions

Write a binary search tree and include the functions to find the number of nodes and...
Write a binary search tree and include the functions to find the number of nodes and the height of the tree. Test your code in main. Print the post-order, in-order and pre-order traversal. in c++ please.
Give a proof by induction that the number of nodes of a full binary tree with...
Give a proof by induction that the number of nodes of a full binary tree with a height of "h" is: (2^(h+1))-1.
Write a O(n) method valuesInLevelOrder() that returns a list of the nodes of a binary tree...
Write a O(n) method valuesInLevelOrder() that returns a list of the nodes of a binary tree in level-order. That is, the method should return the root, then the nodes at depth 1, followed by the nodes at depth 2, and so on. Your algorithm should begin by putting the tree root on an initially empty queue. Then dequeue a node, add it to the output, and enqueue its left and right children (if they exist). Repeat until the queue is...
Write a C++ program will read in the number of nodes and a binary relation representing...
Write a C++ program will read in the number of nodes and a binary relation representing a graph. The program will create an adjacency matrix from the binary relation. The program will then print the following : 1. The adjacency matrix 2. Determine if there are any isolated nodes and print them 3. Determine if an Euler path exists and said so. The sample run of the program is as follows. The output should just like this: It starts by...
In Java Write a method countOddInternalNodes that returns the number of internal nodes in a binary...
In Java Write a method countOddInternalNodes that returns the number of internal nodes in a binary tree that contain odd numbers. You are essentially writing a method that will become part of the IntegerTree class. You may define private helper methods to solve this problem. Make your own trees in the main method of the code. I specifically kept vague the way the nodes are being added to the tree so you can practice building your own trees from scratch...
A binary tree model with 7 decision nodes will have how many terminal nodes?
A binary tree model with 7 decision nodes will have how many terminal nodes?
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes...
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes (data, x, y). The binary tree need to have function "add()" to add new node into the tree. After added all 10 nodes, it will be sorted and turn into a balanced binary search tree.
Assume that a given binary tree stores integer values in its nodes. Write a Java method...
Assume that a given binary tree stores integer values in its nodes. Write a Java method "smallest" that returns the smallest item in the tree.
the following lists the nodes in a binary tree in two different orders: Preorder : A...
the following lists the nodes in a binary tree in two different orders: Preorder : A B C D E F G H I J K L M Inorder : C E D F B A H J I K G M L Draw the binary tree
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the...
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the nodes in a binary tree that have only one child. Convert it to an iterative version. in C++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT