//二叉树建立,以及三种顺序的输出
#include<iostream> #include<cstdio> #include<cstdlib> using namespace std; typedef char ElemType; typedef struct Node { ElemType data; struct Node *lChild; struct Node *rChild; }BiNode, *BiTree; BiTree CreatBiTree() { char ch; BiTree t; ch=getchar(); if(ch=='1') t=NULL; else { t=(BiTree)malloc(sizeof(BiNode)); t->data=ch; t->lChild=CreatBiTree(); t->rChild=CreatBiTree(); } return t; } void preOrder(BiTree t) { if(t) { putchar(t->data); preOrder(t->lChild); preOrder(t->rChild); } } void inOrder(BiTree t) { if(t) { inOrder(t->lChild); putchar(t->data); inOrder(t->rChild); } } void posOrder(BiTree t) { if(t) { posOrder(t->lChild); posOrder(t->rChild); putchar(t->data); } } int main() { BiTree t; t=CreatBiTree(); printf("先序遍历:"); preOrder(t); printf(" "); printf("中序遍历:"); inOrder(t); printf(" "); printf("后序遍历:"); posOrder(t); printf(" "); return 0; }