Input:
The input file contains zero or many lines with descriptions of the tree’s internal nodes in the form:
Parent Child_1 Child_2 … Child_n
…
0
Parent specifies the internal node number of the tree [1..999’999’999]
Child_i defines the i-th child (number [1..999’999’999]) of the < Parent > node
0 – The input file is always terminated by a line containing only the number 0.
Output:
A mirror image of the given tree shall be written to the output file. The tree must be written in the form: internal nodes are in preorder order.
I keep getting these errors: wrong answer 3rd words differ – expected: ‘0’, found: ‘2’;wrong answer 4th words differ – expected: ‘0’, found: ‘3’; wrong answer 102nd words differ – expected: ‘0’, found: ‘100’; wrong answer 99th words differ – expected: ‘0’, found: ’50’; wrong answer 99th words differ – expected: ‘0’, found: ’50’; wrong answer 12th words differ – expected: ‘3’, found: ’11’
#include <iostream>
#include <fstream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <sstream>
#include <unordered_set>
using namespace std;
struct TreeNode {
int val;
vector<int> children;
};
void mirror(TreeNode* node, unordered_map<int, TreeNode*>& nodeMap, ofstream& outputFile) {
if (!node) return;
reverse(node->children.begin(), node->children.end()); // Correctly reverse the children for mirroring
outputFile << node->val;
for (int child : node->children) {
outputFile << " " << child;
}
outputFile << endl;
for (int child : node->children) {
mirror(nodeMap[child], nodeMap, outputFile); // Recursive call to mirror child subtrees
}
}
int main() {
ifstream inputFile("alice.in");
ofstream outputFile("alice.out");
unordered_map<int, TreeNode*> nodeMap;
string line;
while (getline(inputFile, line) && line != "0") {
stringstream ss(line);
int parent;
ss >> parent;
if (nodeMap.find(parent) == nodeMap.end()) {
nodeMap[parent] = new TreeNode{parent};
}
int child;
while (ss >> child) {
nodeMap[parent]->children.push_back(child);
if (nodeMap.find(child) == nodeMap.end()) {
nodeMap[child] = new TreeNode{child};
}
}
}
unordered_set<int> parentNodes;
for (auto& pair : nodeMap) {
for (int child : pair.second->children) {
parentNodes.insert(child);
}
}
int rootVal = -1;
for (auto& pair : nodeMap) {
if (parentNodes.find(pair.first) == parentNodes.end()) {
rootVal = pair.first;
break;
}
}
if (rootVal != -1) {
mirror(nodeMap[rootVal], nodeMap, outputFile);
}
outputFile << "0" << endl; // Ensure this is the only place where '0' is written as a standalone line
inputFile.close();
outputFile.close();
return 0;
}
This is how the outputs should look –
Example 1 (preorder sequence in the input):
Content of the input file alice.in:
1 2 3 4 5
3 6 7
5 8
8 9 10 11
0
Content of alice.out output file:
1 5 4 3 2
5 8
8 11 10 9
3 7 6
0
Example 2 (arbitrary sequence in the input):
Content of input file alice.in:
5 8
3 6 7
8 9 10 11
1 2 3 4 5
0
Content of alice.out output file:
1 5 4 3 2
5 8
8 11 10 9
3 7 6
0
Mārtiņš Ozols is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.