fork download
  1. #include<stdio.h>
  2. #include<string>
  3. #include<iostream>
  4. #include<algorithm>
  5. #include<vector>
  6. #include<map>
  7. using namespace std;
  8. map<char,char> mleft;
  9. map<char,char> mright;
  10. void preorder(char now){
  11. if(now=='.') return;
  12. cout<<now;
  13. preorder(mleft[now]);
  14. preorder(mright[now]);
  15. }
  16. void inorder(char now){
  17. if(now=='.') return;
  18. inorder(mleft[now]);
  19. cout<<now;
  20. inorder(mright[now]);
  21. }
  22. void postorder(char now){
  23. if(now=='.') return;
  24. postorder(mleft[now]);
  25. postorder(mright[now]);
  26. cout<<now;
  27. }
  28. int main(){
  29. int N;
  30. cin>>N;
  31. for(int x=0;x<N;x++){
  32. string input,input2,input3;
  33. cin>>input>>input2>>input3;
  34. mleft[input[0]]=input2[0];
  35. mright[input[0]]=input3[0];
  36. }
  37. preorder('A');
  38. cout<<'\n';
  39. inorder('A');
  40. cout<<'\n';
  41. postorder('A');
  42. }
Success #stdin #stdout 0.01s 5344KB
stdin
7
A B C
B D .
C E F
E . .
F . G
D . .
G . .
stdout
ABDCEFG
DBAECFG
DBEGFCA