树的前序遍历,水题~
unordered_map<char,char> l,r,fa;
set<char> node;
int n;
void preorder(char root)
{
if(root == ‘*‘) return;
cout<<root;
preorder(l[root]);
preorder(r[root]);
}
int main()
{
cin>>n;
string s;
for(int i=0;i<n;i++)
{
cin>>s;
l[s[0]]=s[1];
r[s[0]]=s[2];
fa[s[1]]=fa[s[2]]=s[0];
}
char root=s[1];//or s[2]
while(fa.count(root))
root=fa[root];
preorder(root);
//system("pause");
return 0;
}
原文:https://www.cnblogs.com/fxh0707/p/14322906.html