Skip to content

Instantly share code, notes, and snippets.

@EsProgram
Created April 18, 2014 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EsProgram/11048516 to your computer and use it in GitHub Desktop.
Save EsProgram/11048516 to your computer and use it in GitHub Desktop.
#include<stdio.h>
//#define DEBUG
#ifdef DEBUG
unsigned long long int count = 0;
#endif
struct Node
{
struct Node *preNode;
struct Node *nextNode;
char value;
}root;
void ShowParent(struct Node*);
void Create(struct Node *node, int l, int r);
int main()
{
int l, r;
while (1)
{
printf("¥nInput L and R.¥nIf you enter a negative value, this program will close.¥n");
printf("L = "); scanf("%d", &l);
printf("R = "); scanf("%d", &r);
puts("");
if (l < 0 || r < 0)
return 0;
root.preNode = NULL;
Create(&root, l, r);
#ifdef DEBUG
printf("¥n-----------------------¥ncount = %llu¥n-----------------------¥n¥n¥n¥n", count);
count = 0LL;
#endif
}
}
void ShowParent(struct Node *node)
{
printf("%c", node->value);
if (node->preNode != NULL)
ShowParent(node->preNode);
}
void Create(struct Node *node, int l, int r)
{
struct Node nextNode = { NULL, NULL, '¥0' };
if (l > 0)
{
node->value = 'L';
node->nextNode = &nextNode;
node->nextNode->preNode = node;
Create(node->nextNode, l - 1, r);
}
if (r > 0)
{
node->value = 'R';
node->nextNode = &nextNode;
node->nextNode->preNode = node;
Create(node->nextNode, l, r - 1);
}
if (node->value == 'L' || node->value == 'R')
return;
ShowParent(node->preNode);
#ifdef DEBUG
++count;
#endif
puts("");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment