#include<iostream> #include<queue> using namespace std; #define ElemType char typedef struct BiTNode{ ElemType data; struct BiTNode *lchild,*rchild; }BiTNode,*BiTree; void CreateBiTree(BiTree &T){ char ch;cin>>ch; if(ch==‘#‘){ T=NULL;return; } T=new BiTNode; T->data=ch; CreateBiTree(T->lchild); CreateBiTree(T->rchild); } void LevelOrder(BiTree T){ queue<BiTree> q; q.push(T); BiTree t; while(!q.empty()){ t=q.front(); cout<<t->data<<" "; q.pop(); if(t->lchild)q.push(t->lchild); if(t->rchild)q.push(t->rchild); } cout<<endl; } void PreOrder(BiTree T){ if(T){ cout<<T->data<<" "; PreOrder(T->lchild); PreOrder(T->rchild); } } int main(){ BiTree root; CreateBiTree(root); LevelOrder(root); PreOrder(root); cout<<endl; return 0; }
原文:https://www.cnblogs.com/lyt888/p/12701906.html