#include <bits/stdc++.h>
using
namespace
std;
Â
struct
Node
{
  Â
int
key;
  Â
struct
Node *left, *right;
};
Â
struct
Node *newNode(
int
item)
{
  Â
struct
Node *temp =
new
Node;
   temp->key = item;
   temp->left = temp->right = NULL;
  Â
return
temp;
}
Â
void
preorder(Node *root)
{
  Â
if
(root != NULL)
   {
      Â
printf
(
"%d "
, root->key);
       preorder(root->left);
       preorder(root->right);
   }
}
Â
vector<Node *> getTrees(
int
arr[],
int
start,
int
end)
{
  Â
   vector<Node *> trees;
Â
  Â
  Â
if
(start > end)
   {
       trees.push_back(NULL);
      Â
return
trees;
   }
Â
  Â
  Â
for
(
int
i = start; i <= end; i++)
   {
      Â
       vector<Node *> ltrees = getTrees(arr, start, i-1);
Â
      Â
       vector<Node *> rtrees = getTrees(arr, i+1, end);
Â
      Â
      Â
for
(
int
j = 0; j < ltrees.size(); j++)
       {
          Â
for
(
int
k = 0; k < rtrees.size(); k++)
           {
              Â
               Node * node = newNode(arr[i]);
Â
              Â
               node->left = ltrees[j];
Â
              Â
               node->right = rtrees[k];
Â
              Â
               trees.push_back(node);
           }
       }
   }
  Â
return
trees;
}
Â
int
main()
{
  Â
int
in[] = {4, 5, 7};
  Â
int
n =
sizeof
(in) /
sizeof
(in[0]);
Â
   vector<Node *> trees = getTrees(in, 0, n-1);
Â
   cout <<
"Preorder traversals of different "
        <<
"possible Binary Trees are \n"
;
  Â
for
(
int
i = 0; i < trees.size(); i++)
   {
       preorder(trees[i]);
      Â
printf
(
"\n"
);
   }
  Â
return
0;
}