这个题就是1130的翻版,注意进入的条件。
#include <bits/stdc++.h>
using namespace std;
struct node {
int lc, rc;
string data;
}Node[50];
bool vis[50];
string s = "+-*% ";
string post(int root) {
if (Node[root].lc == -1 && Node[root].rc == -1) return "(" + Node[root].data + ")";
if (Node[root].lc == -1 && Node[root].rc != -1) return "(" + Node[root].data + post(Node[root].rc) + ")";
if (Node[root].lc != -1 && Node[root].rc != -1) return "(" + post(Node[root].lc) + post(Node[root].rc) + Node[root].data + ")";
// if (s.find(Node[root].data) != -1) return "(" + post(root) + ")";
// return post(root) + "(" + Node[root].data + ")";
}
int main() {
int n, lc, rc, root;
string data;
scanf ("%d", &n);
for (int i = 1; i <= n; i++) {
cin >> data >> lc >> rc;
Node[i].lc = lc;
Node[i].rc = rc;
Node[i].data = data;
if (lc != -1) vis[lc] = true;
if (rc != -1) vis[rc] = true;
}
for (int i = 1; i <= n; i++) {
if (vis[i] == false) {
root = i;
break;
}
}
cout << post(root);
}