kk.nitrkl
BAN USER#include <iostream>
using namespace std;
string str="NNLLL";
int i = 0;
typedef struct node
{
char val;
struct node *left;
struct node *right;
}*tree;
tree root;
void construct_tree(tree current)
{
if(i < (int)str.length() - 1 && current->val=='N')
{
tree temp_l = new node;
temp_l->left=temp_l->right=NULL;
temp_l->val= str[++i];
current->left = temp_l;
construct_tree(temp_l);
tree temp_r = new node;
temp_r->left=temp_r->right=NULL;
temp_r->val = str[++i];
current->right = temp_r;
construct_tree(temp_r);
}
return;
}
void print(tree current)
{
if(current != NULL)
{
cout<<current->val<<" ";
print(current->left);
print(current->right);
}
}
int main()
{
tree tmp = new node;
tmp->left=tmp->right=NULL;
root = tmp;
if(str.length()-1 > 0)
{
tmp->val = str[0];
construct_tree(tmp);
}
print(root);
return 0;
}
it works fine but is recursive .. need to think about non-recursive implementation
- kk.nitrkl January 27, 2012