fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. char in[]= {'d','b','e','a','f','c','g'};
  5. char pre[]={'a','b','d','e','c','f','g'};
  6. int size = sizeof(in);
  7. int pos = 0;
  8.  
  9. typedef struct node{
  10. char value;
  11. struct node*left;
  12. struct node*right;
  13. }treenode;
  14.  
  15. treenode* get_tree(int l,int r);
  16. void print(treenode*root);
  17. int search(char val);
  18.  
  19. void print(treenode*root)
  20. {
  21. if(root==NULL)
  22. return;
  23. print(root->left);
  24. printf("%c",root->value);
  25. print(root->right);
  26. }
  27.  
  28. int search(char val)
  29. {
  30. int i;
  31. for(i=0;i<size;i++)
  32. {
  33. if(in[i]==val)
  34. return i;
  35. }
  36. return -1;
  37. }
  38.  
  39. treenode* get_tree(int l, int r)
  40. {
  41. int inpos = search(pre[pos]);
  42. if(l>=r)return NULL;
  43. pos++;
  44. treenode* temp = (treenode*)malloc(sizeof(struct node));
  45. temp->value = in[inpos];
  46. temp->left = get_tree(l,inpos);
  47. temp->right = get_tree(inpos+1,r);
  48. return temp;
  49. }
  50.  
  51. int main()
  52. {
  53.  
  54. treenode*root = NULL;
  55. int l = 0;
  56. root = get_tree(l,size);
  57. print(root);
  58. printf("\n");
  59. return 0;
  60. }
  61.  
Success #stdin #stdout 0s 2856KB
stdin
Standard input is empty
stdout
dbeafcg