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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

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?

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay