广度优先搜索算法(Breadth-First-Search,BFS),又称做宽度优先搜索。BFS算法是从根节点开始,沿着树的宽度遍历树的节点。若是全部节点均被访问,则算法停止。html
一、首先将根节点放入队列中。node
二、从队列中取出第一个节点,并检验它是否为目标。算法
- 若是找到目标,则结束搜索并回传结果。
- 不然将它全部还没有检验过的直接子节点加入队列中。
三、若队列为空,表示整张图都检查过了——亦即图中没有欲搜索的目标。结束搜索并回传“找不到目标”。编程
四、重复步骤2。spa
灰色的是加入队列中的活动节点,黑色是从活动节点队列中选取的扩展节点。.net
每次将一个活动节点标记为扩展节点后,进行判断,并将该点从活动节点队列中移除,再将该节点全部未检测的子节点加入活动节点队列中去。3d
接下来再从队列中选取新的活动节点做为扩展节点,如此循环下去,直至队列为空为止,说明已经遍历过全部的节点。rest
由于全部节点都必须被存储,所以BFS的空间复杂度为 O(|V|+|E|),其中 |V| 是节点的数目,而 |E| 是图中边的数目。code
最差情形下,BFS必须查找全部到可能节点的全部路径,所以其时间复杂度为 O(|V|+|E|)。htm
//Given a binary tree, find its minimum depth. //The minimum depth is the number of nodes along //the shortest path from the root node down to //the nearest leaf node. // 递归方法 class Solution { public: int run(TreeNode *root) { if (!root) { return 0; } // 若左子树为空,返回右子树深度 + 1 if (root->left == nullptr) { return (run(root->right) + 1); } // 若右子树为空,返回左子树深度 + 1 if (root->right == nullptr) { return (run(root->left) + 1); } // 左右子树都不为空,返回较小值 int leftDepth = run(root->left); int rightDepth = run(root->right); return ((leftDepth < rightDepth) ? (leftDepth + 1) : (rightDepth + 1)); } }; // BFS方法 #include <vector> using namespace std; class Solution { public: int run(TreeNode *root) { if (!root) { return 0; } vector <TreeNode *> que; TreeNode *now = root; // 当前访问的节点 TreeNode *last = root; // 每层最后的一个节点 que.push_back(root); int depth = 1; // 初始深度 while (que.size()) { // 取出队列中的第一个节点做为当前节点 now = que.front(); que.erase(que.begin()); // 若是当前节点没有子节点了,直接终止循环,说明是叶子节点,返回最小深度 if ((now->left == nullptr) && (now->right == nullptr)) { break; } // 将子节点加入队列中 if (now->left != nullptr) { que.push_back(now->left); } if (now->right != nullptr) { que.push_back(now->right); } // 当访问到每层的最后一个节点时,深度+1 if (now == last) { depth++; last = que.back(); // 将下一层最后一个节点赋给last } } return depth; } };