先序遍历:根结点,左子树,右子树
中序遍历:左子树,根节点,右子树
后序遍历:左子树,右子树,根节点
1 #include<bits/stdc++.h> 2 using namespace std; 3 int k;string s; 4 void dfs() 5 { 6 if(s[k]=='#') 7 { 8 k++; 9 return ; 10 } 11 char c=s[k++];//根节点 12 //先序输出 13 dfs();//左子树 14 cout<<c<<" ";//中序遍历中间输出 15 dfs();//右子树 16 //后序输出 17 } 18 int main() 19 { 20 cin>>s; 21 dfs(); 22 return 0; 23 }