以下问题是关于二叉树的,情况如下~

输入:abd??e?g??cf???
#include "iostream"
using namespace std;
struct Node
{
char data;
Node*Lchild;
Node*Rchild;
};
class tree
{
public:
tree();
Node*Createtree(Node*T);
void InorderTraverse(Node*T);
void bianli(int i);

private:
Node*head;
};
tree::tree()
{
head=Createtree(head);
cout<<head->data<<" "
<<(head->Lchild)->data;//调试到这里根节点的左右子树没有相应的值
}
Node*tree::Createtree(Node*T)
{
char ch;
cin>>ch;
if(ch=='?')
{
T=NULL;
}
else
{
T=new Node;
T->data=ch;
Createtree(T->Lchild);
Createtree(T->Rchild);
return T;
}
}
void tree::InorderTraverse(Node*T)
{
if(T!=NULL)
{
InorderTraverse(T->Lchild);
cout<<T->data<<" ";
InorderTraverse(T->Rchild);
}
}
void tree::bianli(int i)
{
if(i==1)InorderTraverse(head);
}
int main()
{
tree a;
a.bianli(1);
}

侃侃无极
浏览 162回答 2
2回答

萧十郎

Node*tree::Createtree(){ Node * T=NULL;char ch;cin>>ch;if(ch=='?') return NULL;T=new Node;T->data=ch;T->Lchild=Createtree();T->Rchild=Createtree();return T;}

富国沪深

Createtree函数中ch == '?'处没有返回值?
打开App,查看更多内容
随时随地看视频慕课网APP