<span style="font-size:18px;">#include<iostream>
using namespace std;
typedef char elemtype;
typedef struct Node
{
elemtype data;
Node *Leftchild,*Rightchild;
}Node;
//建立二叉树
Node* creatTree()
{
elemtype ch;
cin>>ch;
Node*T;
if(ch=='#')
T=NULL;
else
{
T=(Node*)malloc(sizeof(Node));
T->data=ch;
T->Leftchild=creatTree();
T->Rightchild=creatTree();
}
return T;
}
void preorder(Node *t)
{
if(t!=NULL)
{
printf("%c ",t->data);
preorder(t->Leftchild);
preorder(t->Rightchild);
}
}
int main()
{
Node *T=creatTree();
preorder(T);
return 0;
}</span>
建立二叉树和输出
最新推荐文章于 2022-09-02 17:16:28 发布