二叉树:由已知两种遍历求未知遍历

来源:互联网 发布:搜集房源信息的软件 编辑:程序博客网 时间:2024/05/21 12:57

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

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;  }
0 0
原创粉丝点击