为啥我二叉树创建好之后,传到遍历方法中,输出就不对呢
贴代码图和问题图
用c语言写的先序创建二叉树,纠结半天了。劳请各位看看
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct TreeNode{
char data;
struct TreeNode *lchild;
struct TreeNode *rchild;
}TreeNode;
void pf(TreeNode *treeList){
printf("根地址:%d\n",treeList);
if(treeList!=NULL){
printf("根结点:%c\n",treeList->data);
pf(treeList->lchild);
pf(treeList->rchild);
}
}
TreeNode* create(TreeNode *treeList){
char value;
printf("请输入一个字符\n");
scanf(" %c",&value);//注意啊,回车也会被当作一个字符
if(value=='#'){
treeList=NULL;
}else{
treeList=(TreeNode *)malloc(sizeof(TreeNode));
treeList->data=value;
printf("输出没问题啊:%c,%d\n",treeList->data,treeList);
create(treeList->lchild);
create(treeList->rchild);
}
return treeList;
}
int main(){
TreeNode *treeList;
treeList=(TreeNode*)malloc(sizeof(TreeNode));
treeList->lchild=NULL;
treeList->rchild=NULL;
treeList=create(treeList);
printf("根结点:%c,%d\n",treeList->data,treeList);
pf(&treeList);
}