---2012---
Problem1:
Solution:
typedef struct LNode{
int data;
struct LNode *next;
}LNode;
int movep(LNode *&L){
LNode *p=L->next;
LNode *pre=L;
L->next=NULL;
LNode *temp=NULL;
int count=0;
while(p!=NULL){
if(p->data<0){
temp=p;
p=p->next;
pre->next=temp;
temp->next=NULL;
pre=temp;
}
if(p->data>0){
count++;
temp=p;
p=p->next;
temp->next=L->next;
L->next=temp;
if(count==1)
pre=temp;
}
}
printf("%d\n",count+1);
return 0;
}
Problem2:
int Width(TreeNode *T){
if(T==NULL)
return 0;
TreeNode *queue[maxsize]=NULL;
TreeNode *node=NULL;
int front=-1,rear=-1,last=0;
int width=1,count=0;
queue[++rear]=NULL;
while(front<rear){
node=queue[++front];
if(node->firstchild!=NULL){
node=node->firstchild;
queue[++rear]=node;
while(node->nextsibling!=NULL){
node=node->nextsibling;
queue[++rear]=node;
}
}
if(front==last){
last=rear;
count=rear-front;
}
if(count>width)
width=count;
}
return width;
}
---2013---
Problem1:
void solve1(LNode *A,LNode *B){
LNode *p=A->next;
LNode *q=B->next;
LNode *temp=NULL;
A->next=NULL;
while(p!=NULL&&q!=NULL){
if(p->data<q->data)
p=p->next;
if(p->data>q->data)
q=q->next;
if(p->data==q->data){
temp=p;
p=p->next;
q=q->next;
temp->next=A->next;
A->next=temp;
}
}
}
void solve11(LNode *A,LNode *B){
LNode *p=A->next;
LNode *q=B->next;
LNode *temp,*pre=A;
int flag=0;
while(p!=NULL){
while(q!=NULL&&flag==0){
if(q->data==p->data)
flag=1;
else
q=q->next;
}
if (flag==0){
pre->next=p->next;
free(p);
p=pre->next;
}
else{
pre=p;
p=p->next;
flag=0;
}
q=B->next;
}
p=A->next;
A->next=NULL;
while(p!=NULL){
temp=p;
p=p->next;
temp->next=A->next;
A->next=temp;
}
}
Problem2:
int Depth(BTNode *T){
BTNode *queue[maxsize]={NULL};
BTNode *node=NULL;
int front=-1,,rear=-1,last=0;
int levle=0;
queue[++rear]=T;
while(front<rear){
node=queue[++front];
if(node->lchild!=NULL)
queue[++rear]=node->child;
if(node->rchild!=NULL)
queue[++rear]=node->rchild;
if(front==last){
last=rear;
++level;
}
}
return level;
}
Problem1:
Solution:
typedef struct BTNode{
int data;
struct BTNode *lchild;
struct BTNode *rchild;
}BTNode;
void delet(BTNode *root){
if (root!=NULL){
delet(root->lchild);
delet(root->rchild);
}
free(root);
}
void Delete(BTNode *T,int x){
BTNode *queue[max]={NULL};
BTNode *node=NULL;
int front=-1,rear=-1,last=0;
queue[++rear]=T;
while(front<rear){
node=queue[++front];
if (node->data===x){
delet(node->lchild);
delet(node->rchild);
free(node);
}
if(node->lchild!=NULL)
queue[++rear]=node->lchild;
if(node->rchild!=NULL)
queue[++rear]=node->rchild;
}
}
Problem2:
Solutio:
void swap(int num[],int n){
int i=0,j=n-1;
int temp=0;
while(i<j){
while(i<j&&num[i]>=60)
++i;
while(i<j&&num[j]<60)
--j;
if(i<j){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
Problem3:
Solution:
typedef struct LNode{
int data;
struct LNode *next;
}LNode;
LNode *merge(LNode *A,LNode *B){
LNode *p=A->next;
LNOde *q=B->next;
A->next=NULL,B->next=NULL;
while(p!=NULL||q!=NULL){
if(q==NULL||(p!=NULL&&p->data>=q->data)){
temp=p;
p=p->next;
}
if(p==NULL||(q!=NULL&&q->data>=p->data)){
temp=q;
q=q->next;
}
if(A->next!=NULL&&temp->data==A->next->data){
free(temp);
}
else{
temp->next=A->next;
A->next=temp;
}
}
free(B);
return A;
}
Problem5:
Solution:
typedef struct BTNode{
int data;
struct BTNode *lchild;
struct BTNode *rchild;
}BTNode;
int Find(BTNode *root,int x1,int x2){
BTNode *queue[maxsize]={NULL};
BTNode *node;
int front=-1,rear=-1,last=0;
int level=0,v=-2,levle_temp=0;
queue[++rear]=root;
while(front<rear){
node=queue[++front];
if(node->lchld!=NULL)
queue[++rear]=node->lchild;
if(node->rchild!=NULL)
queue[++rear]=node->rchild;
if(front==last){
last=rear;
++level;
}
if(node->data==x1||node->data==x2){
++v;
if(v==-1)
level_temp=level;
if(v==0)
v=level-level_temp;
}
}
return v;
}
Problem6:
Solution:
void solvezhenti(LNode *L){
LNode *p=L->next;
LNode *q=L->next;
LNode *pre=L;
LNode *r,*k,*remove=NULL;
int suml,sumr,delta=0,min=INFMax;
while(p!=NULL){
suml=0,sumr=0;
while(q!=p){
suml=suml+q->data;
q=q->next;
}
r=p->next;
while(r!=NULL){
sumr=sumr+r->data;
r=r->next;
}
delta=abs(suml-sumr);
if(delta<min){
min=delta;
k=pre;
}
pre=p;
p=p->next;
q=L->next;
}
remove=k->next;
k->next=k->next->next;
free(remove);
}
Problem7:
Solution:
typedef struct TreeNode{
Elemtype data;
struct TreeNode *firstchild;
struct TreeNode *nextsibling;
}TreeNode;
TreeNode *solve7(TreeNode *root,TreeNode *p){
TreeNode *queue[maxsize]={NULL};
TreeNode *node=NULL,temparent=NULL;
int front=-1,rear=-1,last=0;
queue[++rear]=root;
while(front<rear){
node=queue[++front];
if(node->firstchild!=NULL)
temparent=node;
if(node->firstchild==p)
return temparent;
node=node->firstchild;
while(node->nextsibling!=NULL){
node=node->nextsibling;
if(node==p)
return temparent;
queue[++rear]=node;
}
if(front==last)
last=rear;
}
}