二叉树 已知 两种遍历序列 求第三种遍历序列

来源:互联网 发布:易仙天星择日软件下载 编辑:程序博客网 时间:2024/05/12 01:45

已知 前序和中序遍历 求后序遍历序列

struct node *creat(char *a, char *b, int n)  {   struct node *ptr;   char *p;   int count = 0;   if (n <= 0)     return NULL;    ptr = (struct node *)malloc(sizeof(struct node));    ptr -> data = *a;    for (p = &b[0]; p <= b + n - 1;p ++)    {     if (*p == *a)       break;    }    count = p - b;    ptr -> l = creat(a + 1, b, count);    ptr -> r = creat(a + 1 + count, p + 1, n - 1 - count);   return ptr;  }

已知中序和后序遍历 求前序遍历序列

struct node *creat(char *a, char *b, int n)  {   struct node *ptr;   if (n <= 0)     return NULL;    ptr = (struct node *)malloc(sizeof(struct node));    ptr -> data = b[n - 1];   int q = strchr(a, b[n - 1]) - a;   ptr -> l = creat(a, b, q);   ptr -> r = creat(a + q + 1, b + q, n - q - 1);   return ptr;  }
1 0
原创粉丝点击