1void preorder(Node* root){
2 if(root != NULL){
3 cout<<root->data<<" ";
4 preorder(root->left);
5 preorder(root->right);
6 }
7}
1#include <iostream>
2using namespace std;
3
4class node{
5public:
6 int data;
7 node* left;
8 node* right;
9
10 node(int d){
11 data = d;
12 left = NULL;
13 right = NULL;
14 }
15};
16
17node* buildTree(){
18 int d;
19 cin>>d;
20
21 if(d==-1){
22 return NULL; //to attach a NULL pointer[in case of no child] enter -1
23 }
24 node * root = new node(d);
25 root->left = buildTree();
26 root->right = buildTree();
27 return root;
28}
29
30
31
32//REQUIRED FUNCTION: Inorder Traversal
33
34
35void printIn(node*root){
36 if(root==NULL){
37 return;
38 }
39 //Otherwise Left Root Right
40 printIn(root->left);
41 cout<<root->data<<" ";
42 printIn(root->right);
43}
44
45
46int main(){
47 node* root = buildTree();
48 printIn(root);
49
50 return 0;
51}
52
53//SAMPLE INPUT TO RUN THE CODE ON ANY ONLINE IDE:
54//8 10 1 -1 -1 6 9 -1 -1 7 -1 -1 3 -1 14 13 -1 -1 -1
55