DEV Community

Ankit Rattan
Ankit Rattan

Posted on

InOrder Traversal

Well, here you go for InOrder Traversal...

#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(node* root){
    cout<<"Enter the data: "<<endl;
    int data;
    cin>>data;

    root = new node(data);

    if(data == -1) return NULL;

    cout<<"Enter the data for the left of "<<data<<endl;
    root -> left = buildTree(root -> left);
    cout<<"Enter the data for the right of "<<data<<endl;
    root -> right = buildTree(root -> right);
    return root;
}

void inorder(node* root){
    if(root == NULL) return;

    inorder(root -> left);     // -> Left (L)
    cout<< root -> data << " ";
    inorder(root -> right);  // -> Right (R)

}

int main()
{
    node* root = NULL;
    root = buildTree(root);

    inorder(root);


    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (3)

Collapse
 
pauljlucas profile image
Paul J. Lucas

Just pasting a blob of code with no explanation isn't terribly instructive.

Collapse
 
ankit_rattan profile image
Ankit Rattan

Well! I guess you directly jumped here 😂. I mentioned in my previous post that I will only post codes. !.

Collapse
 
pauljlucas profile image
Paul J. Lucas

And that somehow makes things better?

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more