函数题
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:
- 对queue的巧妙使用:保证队列里母级永远在子级前面
- 函数指针作形参的传递
- QUEUE *head 和QUEUE *tail 的使用
- 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:
- 递归递归递归
- 及时return
- return里面&&和||的使用
- 思维的简洁性:函数直接对当前两个值作比较,不考虑child,child直接放递归
编程题
My Answer