文章
11
粉丝
216
获赞
4
访问
103.1k
创建二叉树时,要把左右孩子的指针指向NULL,以防止样例出现野指针,从而运行错误;
#include<iostream>
using namespace std;
typedef struct node
{
char data;
struct node *lchild, *rchild;
}*Bitree;
string s;
int pos;
void CreatBitree(Bitree &T)
{
if (pos >= s.size()) return;
char c;
c = s[pos++];
if (c == '#') T = NULL;
else
{
T = new node;
T->data = c;
T->lchild = NULL;//or will runing error
T->rchild = NULL;//or will runing error
CreatBitree(T->lchild);
CreatBitree(T->rchild);
}
}
void InOrder(Bitree T)
{
if (T != NULL)
{
InOrder(T->lchild);
cout << T->data << ' ';
InOrder(T->rchild);
}
}
int main()
{
while (cin >> s)
{
Bitree T = NULL;
pos = 0;
CreatBitree(T);
InOrder(T); cout << endl;
}
return 0;
}
登录后发布评论
暂无评论,来抢沙发