笔试算法题(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 题目 给你一个二叉树,请你返 ...
随机推荐
- 关于ArcGis for javascript的使用
1.引用ArcGis for javascript核心类库的两种方式: 1.1.下载js包,解压缩放入项目中 1.1.1.下载核心类库压缩文件, 下载地址: https://developers.ar ...
- ASP.NET Core MVC 打造一个简单的图书馆管理系统 (修正版)(一) 基本模型以及数据库的建立
前言: 本系列文章主要为我之前所学知识的一次微小的实践,以我学校图书馆管理系统为雏形所作. 本系列文章主要参考资料: 微软文档:https://docs.microsoft.com/zh-cn/asp ...
- 使用node.js在sublime text3搭建服务器
问题描述: 使用node.js在sublime text3中搭建好服务器后,第一次使用“ctrl+b”运行服务器没有问题,如图所示 如果想对test.js中的内容做些许修改,保存后再使用“ctrl+b ...
- EditextText输入类型
android:inputType="none"--输入普通字符 android:inputType="text"--输入普通字符 android:inputT ...
- HDU 5558 后缀数组
思路: 这是一个错误的思路, 因为数据水才过= = 首先求出来后缀数组 把rank插到set里 每回差i两边离i近的rank值,更新 如果LCP相同,暴力左(右)继续更新sa的最小值 //By Sir ...
- 洛谷 P3203 [HNOI2010]弹飞绵羊 || bzoj2002
看来这个lct板子的确没什么问题 好像还可以分块做 #include<cstdio> #include<algorithm> using namespace std; type ...
- 递推DP UVA 1291 Dance Dance Revolution
题目传送门 题意:给一串跳舞的动作,至少一只脚落到指定的位置,不同的走法有不同的体力消耗,问最小体力消费多少分析:dp[i][j][k] 表示前i个动作,当前状态(j, k)的最小消费,状态转移方程: ...
- Android偏好设置(5)偏好设置界面显示多个分组,每个分组也有一个界面
1.Using Preference Headers In rare cases, you might want to design your settings such that the first ...
- 转】SparkSQL中的内置函数
原博文来自于: http://blog.csdn.net/u012297062/article/details/52207934 感谢! 使用Spark SQL中的内置函数对数据进行分析,Spa ...
- 可变类型的安全性——更锋利的C#代码小记(2)
ReadOnlyCollection类型是.NET系统类库提供的一个只读集合类型,它与原来的List<string>不存在任何类型转换关系,因此可以从根本上阻止外部对其的修改操作using ...