当日总结
读入用户输入的一串字符串,将字符串按照先序遍历建立一个二叉树。 其中“#”表示的是空链,代表空树。再对建立好的二叉树进行中序遍历,输出遍历结果。
输入格式:
输入1行字符串,长度小于等于100。
输出格式:
输出中序遍历的结果,每个字符后面有一个空格。
输入样例:
在这里给出一组输入。例如:
abc##de#g##f###
输出样例:
在这里给出相应的输出。例如:
c b e g d f a
include
include
using namespace std;
typedef struct treeNode{
char data;
struct treeNode* left;
struct treeNode* right;
}treeNode;
int pos=0;
treeNode buildtree(string s)
{
if(s[pos]=='#')
{
pos++;
return NULL;
}
treeNode node=(treeNode)malloc(sizeof(treeNode));
node->data=s[pos];
pos++;
node->left=buildtree(s);
node->right=buildtree(s);
return node;
}
void inOrder(treeNode root)
{
if(root==NULL)
{
return ;
}
inOrder(root->left);
cout<
inOrder(root->right);
}
int main()
{
string s;
cin>>s;
treeNode* root=buildtree(s);
inOrder(root);
cout<<endl;
return 0;
}

浙公网安备 33010602011771号