Inorder Traversal

Inorder traversal is accessing left then root then right

 

#include <bits/stdc++.h>
using namespace std;

class Node
{
  public:
  int data;
  Node *right, *left;
};

Node *createNode(int new_data)
{
  Node *new_node = new Node();
  new_node->data = new_data;
  new_node->left = NULL;
  new_node->right = NULL;
  return new_node;
}

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

  // Accessing left
  inorder(root->left);

  cout << root->data << " ";

  // Accessing right
  inorder(root->right);
}

int main()
{
  Node *root = createNode(5);

  root1->left = createNode(9);
  root1->right = createNode(11);
  
  root1->left->left = createNode(15);
  root1->left->right = createNode(19);

  return 0;
}

No comments:

If you have any doubt or suggestion let me know in comment section.

Powered by Blogger.