UVa122
层次遍历二叉树。
这道题方便的地方在于不需要构建二叉树,可以直接对输入数据排序然后输出。但是由于输入数据可能导致二叉树不完整(或者节点重复),所以需要对二叉树的完整性进行判断,也就是在对输入节点按照层次遍历的要求排序后,先判断是否存在重复节点,然后再判断从根到每个节点的路径上的节点是否都存在。
#include <iostream>
#include <algorithm>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
struct Node
{
int value;
string path;
Node(const string &pair)
{
string::size_type pos = pair.find(',');
value = stoi(pair.substr(1, pos - 1));
path.assign(pair.substr(pos + 1, pair.length() - pos - 2));
}
};
bool CheckCompleteness(vector<Node> &nodes)
{
if (!nodes.front().path.empty()) {
return false;
}
for (size_t i = 1; i < nodes.size(); i++)
{
if (nodes[i - 1].path == nodes[i].path) {
return false;
}
}
set<string> paths;
for (size_t i = 1; i < nodes.size(); i++)
{
const string &path = nodes[i].path;
for (size_t len = 1; len < path.length() - 1; len++)
{
if (paths.find(path.substr(0, len)) == paths.end()) {
return false;
}
}
paths.insert(path);
}
return true;
}
void traverse(const vector<Node> &nodes)
{
cout << nodes.front().value;
for (size_t i = 1; i < nodes.size(); i++)
{
cout << ' ' << nodes[i].value;
}
cout << endl;
}
int main()
{
vector<Node> nodes;
string line;
while (cin >> line) {
stringstream ss(line);
string pairs;
while (ss >> pairs) {
if (pairs == "()") {
sort(nodes.begin(), nodes.end(), [](const Node &n1, const Node &n2)
{
if (n1.path.length() == n2.path.length())
return n1.path < n2.path;
else
return n1.path.length() < n2.path.length();
});
if (CheckCompleteness(nodes)) {
traverse(nodes);
}
else {
cout << "not complete" << endl;
}
nodes.clear();
}
else {
nodes.emplace_back(pairs);
}
}
}
return 0;
}
/*
(11,LL) (7,LLL) (8,R)
(5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) ()
(3,L) (4,R) ()
*/