+-
先序建立二叉链表及先序中序后序遍历(递归)
#include<iostream>
using namespace std;
typedef struct BiNode{
    char data;
    struct BiNode *lchild,*rchild;
}BiTNode,*BiTree;
//先序建立二叉链表
void CreateBiTree(BiTree &T){    
    char ch;
    cin >> ch;
    if(ch=='#')  T=NULL;
    else{                            
        T=new BiTNode;
        T->data=ch;
        CreateBiTree(T->lchild);
        CreateBiTree(T->rchild);
    }
}
//先序遍历 
void Pre(BiTree T){  
    if(T){
        cout<<T->data;
        Pre(T->lchild);
        Pre(T->rchild);
    }
}
//中序遍历 
void In(BiTree T){  
    if(T){
        In(T->lchild);
        cout<<T->data;
        In(T->rchild);
    }
}
//后序遍历 
void Post(BiTree T){  
    if(T){
        Post(T->lchild);
        Post(T->rchild);
        cout<<T->data;
    }
}
int main(){
    BiTree tree;
    cout<<"先序建立二叉链表:";
    CreateBiTree(tree);
    cout<<"先序遍历的结果为:"; 
    Pre(tree);
    cout<<endl;
    cout<<"中序遍历的结果为:";
    In(tree);
    cout<<endl;
    cout<<"后序遍历的结果为:";
    Post(tree);
    cout<<endl;
}

image