1130. Infix Expression (25)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yhf_naive/article/details/62442414

感想:

水题,中序遍历

#include<iostream>
#include<vector>
#include<map>
#include<deque>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
struct tree{
	string s ;
	tree* left;
	tree* right;
	tree* father;
}t[30];
tree* root;
void inorder(tree* a){
	if(a->right&&a!=root)
	cout<<"(";
	if(a->left)
	inorder(a->left);
	cout<<a->s;
	if(a->right)
	inorder(a->right);
	if(a->right&&a!=root)
	cout<<")";
}
int main(){
	int N,i,k,j;
	string s1;
	cin>>N;
	for(i=1;i<=N;i++){
		cin>>s1>>j>>k;
		t[i].s=s1;
		if(j!=-1){
			t[i].left=&t[j];
			t[j].father=&t[i];
		}
		if(k!=-1){
			t[i].right=&t[k];
			t[k].father=&t[i];
		}
	}
	root=&t[1];
	while(root->father){
		root=root->father;
	}
	inorder(root);
} 

猜你喜欢

转载自blog.csdn.net/yhf_naive/article/details/62442414
今日推荐