层序遍历最后访问谁
运行下面这段程序:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#include <map>
#include <queue>
map<string, vector<string>> TREE = {
{"总部", {"华北", "华南"}},
{"华北", {"北京", "天津"}},
{"华南", {"广州"}},
{"北京", {}}, {"天津", {}}, {"广州", {}},
};
int main() {
queue<string> q;
q.push("总部");
string last;
while (!q.empty()) {
string node = q.front();
q.pop();
last = node;
for (const string& kid : TREE[node]) q.push(kid);
}
cout << last << endl;
}
(本题用 g++ -std=c++17 -O0 编译。)
全部评论