#include #include using namespace std; template class BinaryTreeNode { public: T data; BinaryTreeNode* left; BinaryTreeNode* right; BinaryTreeNode(T data){ this->data=data; left=NULL; right=NULL; } ~BinaryTreeNode(){ delete left; delete right; } }; //printing binary tree void printTree(BinaryTreeNode* root) { if(root==NULL) return; cout<data<<":"; if(root->left){ cout<<"L"<left->data<<" "; } if(root->right){ cout<<"R"<right->data<<" "; } cout<left); printTree(root->right); } //taking input BinaryTreeNode* takeInput(){ int rootData; cout<<"Enter root data"<>rootData; if(rootData==-1) return NULL; BinaryTreeNode* root=new BinaryTreeNode(rootData); BinaryTreeNode* leftChild=takeInput(); BinaryTreeNode* rightChild=takeInput(); root->left=leftChild; root->right=rightChild; return root; } //taking input level wise BinaryTreeNode* takeInputLevelwise(){ int rootData; cout<<"Enter root data"<>rootData; if(rootData==-1) return NULL; BinaryTreeNode* root = new BinaryTreeNode(rootData); queue*> pendingNodes; pendingNodes.push(root); while(pendingNodes.size() != 0){ BinaryTreeNode* front = pendingNodes.front(); pendingNodes.pop(); cout<<"Enter left child of "<data<>leftChildData; if(leftChildData!=-1){ BinaryTreeNode* child = new BinaryTreeNode(leftChildData); front->left=child; pendingNodes.push(child); } cout<<"Enter right child of "<data<>rightChildData; if(rightChildData!=-1){ BinaryTreeNode* child = new BinaryTreeNode(rightChildData); front->right=child; pendingNodes.push(child); } } return root; } //counting nodes int numNodes(BinaryTreeNode* root){ if(root==NULL) return 0; return 1+numNodes(root->left)+numNodes(root->right); }; // 1 2 3 4 5 6 7 -1 -1 -1 -1 8 9 -1 -1 -1 -1 -1 -1 int main() { //normal input //BinaryTreeNode* root=takeInput(); //levelwise input BinaryTreeNode* root=takeInputLevelwise(); cout<<"Total number of nodes: "<