笔试算法题(37):二叉树的层序遍历 & 最长递增的数字串
出题:要求层序遍历二叉树,从上到下的层次,每一层访问顺序为从左到右,并将节点一次编号,输出如下;如果只要求打印指定的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):二叉树的层序遍历 & 最长递增的数字串的更多相关文章
- 刷题-力扣-107. 二叉树的层序遍历 II
107. 二叉树的层序遍历 II 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/binary-tree-level-order-tr ...
- 五三想休息,今天还学习,图解二叉树的层序遍历BFS(广度优先)模板,附面试题题解
壹 ❀ 引 我在从JS执行栈角度图解递归以及二叉树的前.中.后遍历的底层差异一文中,从一个最基本的数组遍历引出递归,在掌握递归的书写规则后,又从JS执行栈角度解释了二叉树三种深度优先(前序.中序后序) ...
- leetcode之二叉树的层序遍历
1.题目描述 2.题目分析 二叉树的层序遍历主要算法思想是使用 队列这一数据结构实现,这个数据结构多应用在和 图相关的算法.例如图的广度优先遍历就可以使用队列的方法实现.本题的关键在于如何识别出一层已 ...
- JS刷算法题:二叉树
Q1.翻转二叉树(easy) 如题所示 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 来源:力扣(LeetCode) ...
- 二叉树的层序遍历 BFS
二叉树的层序遍历,或者说是宽度优先便利,是经常考察的内容. 问题一:层序遍历二叉树并输出,直接输出结果即可,输出格式为一行. #include <iostream> #include &l ...
- 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, ...
- 剑指offer 二叉树的层序遍历
剑指offer 牛客网 二叉树的层序遍历 # -*- coding: utf-8 -*- """ Created on Tue Apr 9 09:33:16 2019 @ ...
- 前端如何应对笔试算法题?(用node编程)
用nodeJs写算法题 咱们前端使用算法的地方不多,但是为了校招笔试,不得不针对算法题去练习呀! 好不容易下定决心 攻克算法题.发现js并不能像c语言一样自建输入输出流.只能回去学习c语言了吗?其实不 ...
- LeetCode 102. 二叉树的层序遍历 | Python
102. 二叉树的层序遍历 题目来源:https://leetcode-cn.com/problems/binary-tree-level-order-traversal 题目 给你一个二叉树,请你返 ...
随机推荐
- Mysql数据库的用户和日志管理
Mysql数据库的用户和日志管理 数据库的用户管理 1.mysql用户账号管理 用户账号 user@host user:账户名称 host:此账户可通过哪些客户端主机请求创建连接线程,可以是ip.主机 ...
- bzoj 1070: [SCOI2007]修车【最小费用最大流】
一开始从客人角度想的,怎么建都不对 从一个修车工所接待的所有顾客花费的总时间来看,设一共有x个人,那么第一个修的对总时间的贡献是x*w1,第二个是(x-1)*w2-以此类推.所以把第i个修车工拆成n组 ...
- Luogu P3916 图的遍历 【优雅的dfs】【内有待填坑】By cellur925
说明 • 对于60% 的数据, n,m在1e3内 • 对于100% 的数据, n,m在1e5内. 本弱弱上来就是一顿暴搜打,dfs n次,每次更新答案,复杂度为O(n*n),果然TLE,60分抱回家. ...
- sigaction函数的功能
sigaction函数的功能是检查或修改与指定信号相关联的处理动作(可同时两种操作). 他是POSIX的信号接口,而signal()是标准C的信号接口(如果程序必须在非POSIX系统上运行,那么就应该 ...
- SQL 初级教程学习(六)
1.创建视图 CREATE VIEW [Current Product List] ASSELECT ProductID,ProductNameFROM ProductsWHERE Discontin ...
- CalService
package org.crazyit.cal; import java.math.BigDecimal; /** * 计算业务类 * * @author yangenxiong yangenxion ...
- 二进制流BinaryFormatter存储读取数据的细节测试
二进制流的使用很方便,为了更好的理解应用它,我创建简单对象开始测试它的增加特性和减少特性. [Serializable] class Data----------开始时候的存储对象 { public ...
- jacaScript数组
1.var arr=['1','2','3'] typeof arr (判断数组类型) print(arr)打印数组内容 2.arr[100]='x', 数组中间自动添加,alert(arr. ...
- 在spring data jpa中使用自定义转换器之使用枚举转换
转载请注明http://www.cnblogs.com/majianming/p/8553217.html 在项目中,经常会出现这样的情况,一个实体的字段名是枚举类型的 我们在把它存放到数据库中是需要 ...
- Windowsforms 中对文件操作
文件及文件夹操作: 引用命名空间:using system .IO; 1.File类: 创建:File.Create(路径);——返回FileStream FileStream fs = File.C ...