#include "iostream"
#include "Memory.h"
using namespace Std;
/*
Find the Root method:
Number of inputs per node: The root node is entered once when it enters itself,
A non-root node is entered once when the parent node is entered, and then entered when you enter
Once, so I entered it two times. So you can calculate each node through the counter
The number of times it was entered to find the root node.
*/
const int MAX = 1002;
struct node{
int id;
Char Val;
int left, right;
Node (char c, int i = 0) {
id = i;
val = c;
left = 0;
right = 0;
}
Node () {
left = 0;
right = 0;
}
};
void preorder (int root, Node *node) {
if (root! = 0) {
cout << Node[root].val;
Preorder (node[root].left, node);
Preorder (node[root].right, node);
}
}
int main () {
int n;
while (CIN >> N) {
int Isroot[max];
Node Node[max];
memset (isroot, 0, sizeof (isroot));
int ID, left, right;
char value;
for (int i = 0; i < n; i++) {
CIN >> ID >> value >> left >> right;
Node[id].id = ID;
Node[id].val = value;
Node[id].left = left;
Node[id].right = right;
isroot[id]++;
isroot[left]++;
isroot[right]++;
}
int root;
for (int i = 0; i < MAX; i++) {
If (Isroot[i]% 2 = = 1) {//number of times the node is the root
root = i;
}
}
Preorder (root, node);
cout << Endl;
}
return 0;
}
1156. Binary tree root finding method with two forks