Skip to content

Instantly share code, notes, and snippets.

@kamalbanga
Last active December 27, 2015 11:59
Show Gist options
  • Save kamalbanga/7322733 to your computer and use it in GitHub Desktop.
Save kamalbanga/7322733 to your computer and use it in GitHub Desktop.
Create Binary Tree from it's PostOrder when leaf nodes are differentiable from internal nodes and internal nodes have exactly two children.
#include <stdio.h>
#include <stdlib.h>
struct node
{
char c;
struct node* left, *right;
};
struct node* createNode(char c)
{
struct node* n = (struct node*)malloc(sizeof(struct node));
n->c = c;
n->left = n->right = NULL;
return n;
}
int createTree(struct node* n, char *S, int i)
{
int j;
n->right = createNode(S[i]);
if (S[i] <= 'z' && S[i] >= 'a')
j = i-1;
else
j = createTree(n->right,S,i-1);
n->left = createNode(S[j]);
if (S[j] <= 'z' && S[j] >= 'a')
return j-1;
else
return createTree(n->left,S,j-1);
}
int main() {
int K;
scanf("%d",&K);
char S[K];
scanf("%s",S);
struct node* root = createNode(S[K-1]);
createTree(root,S,K-2);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment