#include <iostream> using namespace std; class node { public: int data; node* left; node* right; node(int d) { this->data = d; this->left = NULL; this->right = NULL; } }; node* buildTree() { cout << "Enter the data (-1 for no node): "<<endl; int data; cin >> data; if (data == -1) { return NULL; } node* root = new node(data); // Create a new node cout << "Enter data for left child of " << data << ": "<<endl; root->left = buildTree(); cout << "Enter data for right child of " << data << ": "<<endl; root->right = buildTree(); return root; } int main() { node* root = buildTree(); // Create the tree starting from root return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter