Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function. This function takes a node that is a root of a tree, traverses

ID: 3753769 • Letter: W

Question

Write a function. This function takes a node that is a root of a tree, traverses the tree and returns the number of leaves in the tree.

Write a function int countLeaves (Node node) This function takes a node that is a root of a tree, traverses the tree and returns the number of leaves in the tree. The first input in test cases is the number of nodes. The second input is nodes of a tree which are inserted into a binary search tree that order. You dont need to implement insert. You have access to the root of the constructed Binary Search Tree. We will create the tree for you, we will call your method to and output the results. The output is the number of leaves in the tree. We have defined the following C++ Node class for you. The name serves as the value. class Node public: std::string name; Node leftNULL; Node* rightNULL bi 3; Sample Input 1: 2 0 1 Sample Output 1: Sample Input 2: 6 67214 3 Sample Output 2:

Explanation / Answer

int countLeaves(Node *head) { if(head == NULL) { return 0; } else { int count = 0; // add 1 to result only if node is a leaf. if(head->left == NULL && head->right == NULL) { count = 1; } return count + countLeaves(head->left) + countLeaves(head->right); } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote