Inorder Traversal

Mujahida Joynab - Feb 11 - - Dev Community

`


#include
using namespace std;

class Node {
public:
int val;
Node* left;
Node* right;

// Constructor to initialize the node
Node(int value) {
    val = value;
    left = NULL;
    right = NULL;
}
Enter fullscreen mode Exit fullscreen mode

};

// Inorder traversal (Left, Root, Right)
void inorder(Node *root) {
if (root == NULL)
return;

inorder(root->left);
cout << root->val << " ";
inorder(root->right);
Enter fullscreen mode Exit fullscreen mode

}

int main() {
// Creating nodes
Node* root = new Node(10);
Node* a = new Node(20);
Node* b = new Node(30);
Node* c = new Node(40);

// Constructing the tree
root->left = a;
root->right = b;
a->left = c;

// Performing inorder traversal
cout << "Inorder Traversal: ";
inorder(root);
cout << endl;

return 0;
Enter fullscreen mode Exit fullscreen mode

}

`

`
`

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .