Question
Write a public member function incrementNodes for a BinaryTree class that will add 1 to each item in the binary tree. If this function calls any other function, you must write the code for that function as well. Note that the class BinaryTree has a private member variable root which is a Node pointer.
Explanation / Answer
#include using namespace std; struct Node { int data; Node *left; Node *right; }; class BinaryTree { private: Node *root; void incrementNodes(Node *temp); public: void incrementNodes(); }; // Below is the code that you need void BinaryTree::incrementNodes() { incrementNodes(root); } void BinaryTree::incrementNodes(Node *temp) { if(temp != NULL) { temp->data += 1; // increment data in node incrementNodes(temp->left); // recursive call to left subtree incrementNodes(temp->right); // recursive call to right subtree } } // Above is the code that you need int main() { return 0; }