出题:要求层序遍历二叉树,从上到下的层次,每一层访问顺序为从左到右,并将节点一次编号,输出如下;如果只要求打印指定的level的节点,应该如何实现。
  a
  b  c
  d  e  f  g
  h  i
 

分析:

  • 原始的层序遍历类似于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 ;
}

笔试算法题(37):二叉树的层序遍历 & 最长递增的数字串的更多相关文章

  1. 刷题-力扣-107. 二叉树的层序遍历 II

    107. 二叉树的层序遍历 II 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/binary-tree-level-order-tr ...

  2. 五三想休息,今天还学习,图解二叉树的层序遍历BFS(广度优先)模板,附面试题题解

    壹 ❀ 引 我在从JS执行栈角度图解递归以及二叉树的前.中.后遍历的底层差异一文中,从一个最基本的数组遍历引出递归,在掌握递归的书写规则后,又从JS执行栈角度解释了二叉树三种深度优先(前序.中序后序) ...

  3. leetcode之二叉树的层序遍历

    1.题目描述 2.题目分析 二叉树的层序遍历主要算法思想是使用 队列这一数据结构实现,这个数据结构多应用在和 图相关的算法.例如图的广度优先遍历就可以使用队列的方法实现.本题的关键在于如何识别出一层已 ...

  4. JS刷算法题:二叉树

    Q1.翻转二叉树(easy) 如题所示 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 来源:力扣(LeetCode) ...

  5. 二叉树的层序遍历 BFS

    二叉树的层序遍历,或者说是宽度优先便利,是经常考察的内容. 问题一:层序遍历二叉树并输出,直接输出结果即可,输出格式为一行. #include <iostream> #include &l ...

  6. Leetcode 102. Binary Tree Level Order Traversal(二叉树的层序遍历)

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  7. 剑指offer 二叉树的层序遍历

    剑指offer 牛客网 二叉树的层序遍历 # -*- coding: utf-8 -*- """ Created on Tue Apr 9 09:33:16 2019 @ ...

  8. 前端如何应对笔试算法题?(用node编程)

    用nodeJs写算法题 咱们前端使用算法的地方不多,但是为了校招笔试,不得不针对算法题去练习呀! 好不容易下定决心 攻克算法题.发现js并不能像c语言一样自建输入输出流.只能回去学习c语言了吗?其实不 ...

  9. LeetCode 102. 二叉树的层序遍历 | Python

    102. 二叉树的层序遍历 题目来源:https://leetcode-cn.com/problems/binary-tree-level-order-traversal 题目 给你一个二叉树,请你返 ...

随机推荐

  1. 【411】COMP9024 Assignment1 问题汇总

    1. 构建 Makefile 文件后运行错误,undefined reference to 'sqrt' 实际上是没有链接math数学库,所以要 $gcc test.c –lm //-lm就是链接到m ...

  2. thinkphp不能够将ueditor中的html文本显示

    因为这个问题花费了我好长时间,非常的急躁.fuck!! 这次我首先在富文本框中输入了一些文本,这些文本是带有样式的,比如是代码.然后存入数据库,但是当我再一次将它取出来打算放入富文本框中的时候,马丹, ...

  3. Eclipse如何打开Android工程(转载)

    转自:http://www.cnblogs.com/kernel-style/p/3339102.html 一.Eclipse如何打开Android工程 1.你可以在file->new-> ...

  4. BOA服务器搭建步骤

    1.下载Boa Webserver的源码 http://www.boa.org/ 2.解压并编译Boa Webserver $ tar xvf boa-0.94.13.tar.gz 由于Boa Web ...

  5. js实现打字效果

    <!DOCTYPE html> <html> <head> <meta charset='utf-8'> <title>js typing& ...

  6. html 解决空格显示问题

    前端开发者都知道,在html中手动输入多个空格或者是回车,在页面解析的时候都被解析成一个空白显示,但有时候的需求要求显示多个空格,这个问题怎么解决呢?根绝我个人的经验,目前找到了以下集中解决办法: 1 ...

  7. 字体使用sp、dp的区别

    Android设置字体大小, 该用sp还是dp? 大部分人肯定脱口而出, 用sp啊, 傻瓜都知道要用sp而不是dp!!! 那么为什么呢? 可能有人会说, 是google官方专门定义了sp这个单位来描述 ...

  8. 2017 JUST Programming Contest 3.0 B. Linear Algebra Test

    B. Linear Algebra Test time limit per test 3.0 s memory limit per test 256 MB input standard input o ...

  9. Hibernate3中重复引用hbm文件错误信息记录

    Hibernate3中重复引用hbm文件错误信息记录. 八月 ::, ERROR - Context initialization failed org.springframework.beans.f ...

  10. Service官方教程(7)Bound Service示例之1-同进程下直接继承Service

    Extending the Binder class If your service is used only by the local application and does not need t ...