广度优先搜索:
应用一:层序遍历
应用二:最短路径
典型题例:
地上有一个 m 行和 n 列的方格,横纵坐标范围分别是 0∼m−1 和 0∼n−1。
一个机器人从坐标 (0,0) 的格子开始移动,每一次只能向左,右,上,下四个方向移动一格。
但是不能进入行坐标和列坐标的数位之和大于 k 的格子。
请问该机器人能够达到多少个格子?
示例 :
输入:k=18, m=40, n=40输出:1484解释:当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。
思路
(BFS)O(nm)(BFS) O(nm)(BFS)O(nm)
这是一个典型的宽度优先搜索问题,我们从 (0,0)(0, 0)(0,0) 点开始,每次朝上下左右四个方向扩展新的节点即可。
扩展时需要注意新的节点需要满足如下条件:
最后答案就是所有遍历过的合法的节点个数。
时间复杂度
每个节点最多只会入队一次,所以时间复杂度不会超过方格中的节点个数。
最坏情况下会遍历方格中的所有点,所以时间复杂度就是 O(nm)O(nm)O(nm)。
代码:
class Solution {
public:int get_sum(pair p){int s = 0;while(p.first){s += p.first % 10;p.first /= 10;}while(p.second){s += p.second % 10;p.second /= 10;}return s;}int movingCount(int threshold, int rows, int cols){if(!rows || !cols) return 0;queue> q;vector> st(rows, vector(cols, false));int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};int res = 0;q.push({0, 0});while(q.size()){auto t = q.front();q.pop();if(st[t.first][t.second] || get_sum(t) > threshold) continue;res ++;st[t.first][t.second] = true;for(int i = 0; i < 4; i ++){int x = t.first + dx[i], y = t.second + dy[i];//printf("x = %d, y = %d, res = %d\n", x, y, res);if(x >= 0 && x < rows && y >= 0 && y < cols) q.push({x, y});}}return res;}
};
典型题例:
请实现一个函数按照之字形顺序从上向下打印二叉树。
即第一行按照从左到右的顺序打印,第二层按照从右到左的顺序打印,第三行再按照从左到右的顺序打印,其他行以此类推。
示例 :
输入如下图所示二叉树[8, 12, 2, null, null, 6, 4, null, null, null, null]8/ \12 2/ \6 4
输出:[[8], [2, 12], [6, 4]]
思路
核心:
(BFS)O(n)(BFS) O(n)(BFS)O(n)
宽度优先遍历,一层一层来做。即:
时间复杂度
树中每个节点仅会进队出队一次,所以时间复杂度是 O(n)O(n)O(n)。
代码:
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public:vector> printFromTopToBottom(TreeNode* root) {vector> res;if (!root) return res;queue q;q.push(root);q.push(nullptr);vector level;bool zigzag = false; //为false时不反转数组while (q.size()){auto t = q.front();q.pop();if (!t){ if(level.empty()) break;if(zigzag) reverse(level.begin(), level.end()); //反转数组res.push_back(level);level.clear();q.push(nullptr);zigzag = !zigzag; //标记反转continue;}level.push_back(t->val);if (t->left) q.push(t->left);if (t->right) q.push(t->right);}return res;}
};
充电站
推荐一个零声学院免费公开课程,个人觉得老师讲得不错,分享给大家:Linux,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,TCP/IP,协程,DPDK等技术内容,立即学习