DEV Community

Mujahida Joynab
Mujahida Joynab

Posted on

Inorder Traversal

`


#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

}

`

`
`

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

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

Okay