出题:要求层序遍历二叉树,从上到下的层次,每一层访问顺序为从左到右,并将节点一次编号,输出如下;如果只要求打印指定的level的节点,应该如何实现。
  a
  b c
  d e f g
  h i
 笔试算法题(37):二叉树的层序遍历 & 最长递增的数字串-LMLPHP

分析:

  • 原始的层序遍历类似于BFS,打印当前访问的节点curNode的序列号,并将其直接子节点放入队列queue中,然后从queue中取出下一个节点,直 到队列为空;此方法仅能按照层序打印所有节点,并不能区分每一层节点的数量;如果需要区分当前层次的节点,和当前层次节点的子节点,可以使用两个队列 queue1和queue2作为两个缓冲,当访问queue1中的节点时,当前节点的所有子节点放入queue2中,直到queue1队列为空,然后访问 queue2队列的节点,将对应的子节点放入queue1中;最终结束条件为queue1和queue2都为空;
  • 如果需要打印指定level的所有节点,可以再增加一个计数,也就是一次完全访问队列(queue1或者queue2)计数1,第一层为0,直到用户指定的level才进行打印,否则节点的序列号不打印;

解题:

 struct Node {
int value;
Node *left;
Node *right;
}; class MyQueue {
private:
Node *array[];
int capability;
int length;
int head;
int tail;
public:
MyQueue(int n=): array((Node**)malloc(sizeof(Node*)*n)), head(),tail(),capability(n), length() {}
~MyQueue() {delete [] array;} bool isFull() {
if(length == capability) return true;
return false;
}
bool isEmpty() {
if(length == ) return true;
return false;
}
int freeSlot() {
return capability-length;
}
void setBack() {
length=;
}
/**
* head当前的指向位置是下一次将push的元素的
* */
bool push(Node *n) {
if(isFull()) return false;
array[head]=n; head=(head+)%(capability);
length++;
return true;
}
/**
* tail当前指向的位置是下一次将pop的元素的
* */
Node* pop() {
if(isEmpty()) return false;
int temp=tail;
tail=(tail+)%(capability);
length--;
return array[tail];
} void showQueue() {
int i=tail;
int temp=length;
printf("\ncurrent queue elements: \n");
while(temp>) {
printf("%d, ",array[i++]->value);
temp--;
}
}
}; void HierarchyPrint(Node *root) {
MyQueue *queue1=new MyQueue();
MyQueue *queue2=new MyQueue(); int level=-;
queue1->push(root);
Node *temp; while(true) {
level++;
printf("Level %d:\n",level); while(!queue1->isEmpty()) {
temp=queue1->pop();
printf("%d\t",temp->value);
if(temp->left!=NULL)
queue2->push(temp->left);
if(temp->right!=NULL)
queue2->push(temp->right);
} level++;
printf("Level %d:\n",level); while(!queue2->isEmpty()) {
temp=queue2->pop();
printf("%d\t",temp->value);
if(temp->left!=NULL)
queue1->push(temp->left);
if(temp->right!=NULL)
queue1->push(temp->right);
}
}
}

出题:给定一个字符串,要求找到其中最长递增的数字串,给定函数接口如下:
      int ContinuMax(char *input, char **output)
   input为输入的字符串,要求返回连续最长数字串的长度,并将起始位置赋值给output;

分析:看似简单的一个功能实现其实需要注意不少问题;

解题:

 /**
* 注意当数字序列递减又递增的情况,如何设置tempMax的值
* 注意判定第一个是数字的字符位置
* */
int LIN(char *input, char **output) {
int max=,tempMax=;
char *index=input; while(*index!='\0') {
if(*index<''||*index>'') {
/**
* 如果是非数字,则将tempMax清零
* */
tempMax=;
}else if(*(index-)>'' && *(index-)<'') {
/**
* 此处需要保证当前index是数字,并且index-1也
* 需要是数字
* */
if(*index - *(index-)>=) {
tempMax++;
if(max<tempMax) {
max=tempMax;
*output=index-tempMax+;
}
} else {
/**
* 如果数字序列有递减的,将tempMax设为1
* */
tempMax=;
}
} else {
/**
* 当index为数字,index-1为非数字的情况
* */
tempMax=;
}
index++;
}
return max;
} int main() {
char array[]="abcd12dfgr298679rg13";
char *output=NULL;
int length=LIN(array, &output);
if(output==NULL) {
printf("\nthere is no int in array.");
return ;
}
for(int i=;i<length;i++) {
printf("%c",output[i]);
}
return ;
}
05-11 13:19