题目:
代码:
#include
using namespace std;
typedef struct Binarynode
{
char data;
struct Binarynode *left;
struct Binarynode *right;
}BT;//建立二叉树结构体
void Create(BT* &ps)//采用传参传引用的方式
{
char x;
scanf("%c", &x);
if (x == '#')//如果x等于#则将指针置为空
ps = NULL;
else
{
ps = (BT*)malloc(sizeof(BT));//不为空则给ps开辟一个新空间
ps->data = x;
Create(ps->left);
Create(ps->right);//利用先序循环条件去迭代
}
}
void PrintNode(BT* ps)
{
if (ps == NULL)
return;//等于NULL时直接返回
else
{
PrintNode(ps->left);
printf("%c", ps->data);//利用中序打印
PrintNode(ps->right);
}
}
int main()
{
BT* p;
Create(p);
PrintNode(p);
return 0;
}
题目:
代码:
#include
using namespace std;
typedef struct Binarynode
{
char data;
struct Binarynode *left;
struct Binarynode *right;
}BT;//建立二叉树结构体
void Create(BT* &ps)//采用传参传引用的方式
{
char x;
scanf("%c", &x);
if (x == '#')//如果x等于#则将指针置为空
ps = NULL;
else
{
ps = (BT*)malloc(sizeof(BT));//不为空则给ps开辟一个新空间
ps->data = x;
Create(ps->left);
Create(ps->right);//利用先序循环条件去迭代
}
}
void PrintNode(BT* ps)
{
if (ps == NULL)
return;//等于NULL时直接返回
else
{
PrintNode(ps->left);
PrintNode(ps->right);
printf("%c", ps->data);//利用后序打印
}
}
int main()
{
BT* p;
Create(p);
PrintNode(p);
return 0;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)