标签:
递归
#include <iostream> #include <vector> #include <sstream> #include <string> #include <cmath> #include <stack> using namespace std; int pan[17]={0}; int l[17]; int r[17]; ///先根访问节点, ///对节点是访问 对树是遍历子树 ///最简单的访问shi输出 void qianxu(int i) { cout<<i<<" "; if(l[i]) qianxu(l[i]); if(r[i]) qianxu(r[i]); return ; } void zhongxu(int i) { if(l[i]) zhongxu(l[i]); cout<<i<<" "; if(r[i]) zhongxu(r[i]); return ; } void houxu(int i) { if(l[i]) houxu(l[i]); if(r[i]) houxu(r[i]); cout<<i<<" "; return ; } int main() { int n; cin>>n; for(int i=1;i<=n;++i) { cin>>l[i]>>r[i]; } qianxu(1); cout<<endl; zhongxu(1); cout<<endl; houxu(1); cout<<endl; return 0; }
标签:
原文地址:http://www.cnblogs.com/weiweiyi/p/5124345.html