[Problem]
求一棵二叉树的前序遍历,中序遍历和后序遍历[Solution]
#include <iostream> using namespace std;int n; int tree[20][2];/*** pre-order*/ void preOrder(int x){if(x == 0){return;}cout << x << " ";preOrder(tree[x][0]);preOrder(tree[x][1]); }/*** in-order*/ void inOrder(int x){if(x == 0){return;}inOrder(tree[x][0]);cout << x << " ";inOrder(tree[x][1]); }/*** post-order*/ void postOrder(int x){if(x == 0){return;}postOrder(tree[x][0]);postOrder(tree[x][1]);cout << x << " "; }/*** main*/ int main(){// get datacin >> n;for(int i = 1; i <= n; ++i){cin >> tree[i][0] >> tree[i][1];}// pre-orderpreOrder(1);cout << endl;// in-orderinOrder(1);cout << endl;// post-orderpostOrder(1);cout << endl;return 0; }