1130 Infix Expression

#include <iostream>
using namespace std;
const int maxn = 25;
struct node{
	string op;
	int lchild, rchild;
}Node[maxn];
int isRoot[maxn] = {0};
int n, root = 1; 
void inorder(int r){
	if(r == -1) return;
	if(r != root && (Node[r].lchild != -1 || Node[r].rchild != -1)) printf("(");
	inorder(Node[r].lchild);
	cout << Node[r].op;
	inorder(Node[r].rchild);
	if(r != root && (Node[r].lchild != -1 || Node[r].rchild != -1)) printf(")");
}
 main(){
	cin >> n;
	for(int i = 1; i <= n; i++){
		string a; int b, c;
		cin >> a >> b >> c;
		Node[i].op = a; Node[i].lchild = b; Node[i].rchild = c;
		if(b != -1) isRoot[b] = 1;
		if(c != -1) isRoot[c] = 1;
	}
	while(isRoot[root] == 1) root++;
	inorder(root);
	return 0;
}
1130 Infix Expression1130 Infix Expression J_北冥有鱼 发布了153 篇原创文章 · 获赞 0 · 访问量 1365 私信 关注
上一篇:1130 Infix Expression (25 分)


下一篇:Navicat连接Ubuntu中的MySQL,报错1130-host