输入# 代表此结点下子树没有数据,也就是不再继续递归创建
前序创建二叉树
#include <iostream>
#include <cstdlib>
using namespace std;
typedef struct node{
char data;
struct node *left;
struct node *right;
}Bintree;
Bintree *Creat()//前序创建
{
Bintree *bt;
char ch;
cin >> ch;
if(ch == '#')
bt = NULL;
else{
bt = (Bintree*)malloc(sizeof(Bintree*));
bt->data = ch;
bt->left = Creat();
bt->right = Creat();
}
return bt;
}
void PreorderTravelsal(Bintree *bt)
{//前序遍历
if(bt){
cout << bt->data << " ";
PreorderTravelsal(bt->left);
PreorderTravelsal(bt->right);
}
}
int main()
{
Bintree *T;
T = Creat();
PreorderTravelsal(T);
}
输入:ABDG##H###CE#I##F##
输出:A B D G H C E I F
|