FDS - Week 4

来源:互联网 发布:印度制造业数据 编辑:程序博客网 时间:2024/06/07 11:34

函数题


这里写图片描述

My Answer

void Level_order ( Tree T, void (*visit)(Tree ThisNode) ){    if(T == NULL)        return;    typedef struct queue    {      Tree tree;      struct queue* next;     }QUEUE;    QUEUE *Q = (QUEUE*)malloc(sizeof(QUEUE));    Q->tree = T;    Q->next = NULL;     QUEUE *head = Q;    QUEUE *tail = Q;    while(head!=NULL)    {        if(head->tree->Left != NULL)        {            QUEUE *p = (QUEUE*)malloc(sizeof(QUEUE));            p->tree = head->tree->Left;            p->next = NULL;            tail->next = p;            tail = p;        }        if(head->tree->Right != NULL)        {            QUEUE *q = (QUEUE*)malloc(sizeof(QUEUE));            q->tree = head->tree->Right;            q->next = NULL;            tail->next = q;            tail = q;        }        visit(head->tree);        head=head->next;    }} 

Note:

  1. 对queue的巧妙使用:保证队列里母级永远在子级前面
  2. 函数指针作形参的传递
  3. QUEUE *head 和QUEUE *tail 的使用
  4. if条件里判断哪个值是否为NULL

这里写图片描述
My Answer

int Isomorphic( Tree T1, Tree T2 ){    int iso = 1;    if(T1==NULL&&T2==NULL)        return 1;    if(T1==NULL||T2==NULL)        return 0;    if(T1->Element!=T2->Element)        return 0;    return((Isomorphic(T1->Left,T2->Left)&&Isomorphic(T1->Right,T2->Right))||(Isomorphic(T1->Left,T2->Right)&&Isomorphic(T1->Right,T2->Left)));}

Note:

  1. 递归递归递归
  2. 及时return
  3. return里面&&和||的使用
  4. 思维的简洁性:函数直接对当前两个值作比较,不考虑child,child直接放递归

编程题

这里写图片描述
My Answer

0 0
原创粉丝点击