二叉树递归创建和递归遍历

来源:互联网 发布:上市银行现金流数据 编辑:程序博客网 时间:2024/06/05 16:49
/*tips:最后返回指针很重要 bt root=NULL;root=Create(root);*/
#include<iostream>using namespace std;typedef struct Btree{char data;Btree *lchild,*rchild;}Btree,*bt;bt Create(bt T);void DisplayTree(bt T);void main(){bt root=NULL;root=Create(root);DisplayTree(root);}bt Create(bt T){char ch;cin>>ch;if(ch=='#')T=NULL;else{T=new Btree;T->data=ch;T->lchild=Create(T->lchild);T->rchild=Create(T->rchild);}return T;}void DisplayTree(bt T){if(T!=NULL){cout<<T->data<<'\t';DisplayTree(T->lchild);DisplayTree(T->rchild);}}

0 0