笔试算法题(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 题目 给你一个二叉树,请你返 ...
随机推荐
- J20170520-ts
手取り 净收入,实收额;用手抓住,到手
- bzoj 1742: [Usaco2005 nov]Grazing on the Run 边跑边吃草【区间dp】
挺好的区间dp,状态设计很好玩 一开始按套路设f[i][j],g[i][j]为吃完(i,j)区间站在i/j的最小腐败值,后来发现这样并不能保证最优 实际上是设f[i][j],g[i][j]为从i开始吃 ...
- bzoj 2811: [Apio2012]Guard【线段树+贪心】
关于没有忍者的区间用线段树判就好啦 然后把剩下的区间改一改:l/r数组表示最左/最右没被删的点,然后删掉修改后的左边大于右边的:l升r降排个序,把包含完整区间的区间删掉: 然后设f/g数组表示i前/后 ...
- spoj 287 NETADMIN - Smart Network Administrator【二分+最大流】
在spoj上用题号找题就已经是手动二分了吧 把1作为汇点,k个要入网的向t连流量为1的边,因为最小颜色数等于最大边流量,所以对于题目所给出的边(u,v),连接(u,v,c),二分一个流量c,根据最大流 ...
- P5107 能量采集
传送门 官方题解 话说最后的答案忘记取模了结果连暴力都挂了可海星-- //minamoto #include<bits/stdc++.h> #define R register #defi ...
- jQuery html操作
jQuery 拥有可操作 HTML 元素和属性的强大方法. jQuery DOM 操作 DOM = Document Object Model(文档对象模型) jQuery 中非常重要的部分,就是操作 ...
- how-to-fix-fs-re-evaluating-native-module-sources-is-not-supported-graceful
http://stackoverflow.com/questions/37346512/how-to-fix-fs-re-evaluating-native-module-sources-is-not ...
- JDBC中Oracle的SID和ServiceName两种方式的连接字符串格式
SID格式: jdbc:oracle:thin:@<host>:<port>:<SID> 如: jdbc:oracle:thin:@192.168.1.1:1521 ...
- MyEclipse去除不必要的validation
MyEclipse在构建项目时去除不必要的Valication可以加快构建速度. 操作: Window->Perferences->MyEclipse->Validation 在Va ...
- MFC显示文本文档 分类: MFC 2014-12-30 10:03 457人阅读 评论(1) 收藏
新建基于对话框的MFC应用程序.资源视图的对话框上添加编辑框(Edit Control)和按钮(Button), 将编辑框属性:Mutiline.Auto HScroll.Auto VScroll设为 ...