Last active
December 27, 2015 11:59
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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