问题描述
- @数据结构大神:递归遍历二叉树,建立树的代码 为什么错?
-
//创建-输入-打印-递归 # include<stdio.h> # include<stdlib.h> # include<malloc.h> typedef struct Node{ char data; struct Node *Lchild; struct Node *Rchild; }BiTNode,*BiTree; BiTree CreateBiTree(BiTree bt) { char data;int flag=1; while(flag) { scanf("%c",&data); if(data!='$') { bt=(BiTNode *)malloc(sizeof(BiTNode)); bt->data=data; bt-Rchild=(BiTNode *)malloc(sizeof(bt-Rchild)); bt->Rchild=CreateBiTree(bt->Rchild); //为啥建立完右孩子之后就return了呢? bt-Lchild=(BiTNode *)malloc(sizeof(bt-Lchild)); bt->Lchild=CreateBiTree(bt->Lchild);//为啥return后才建立左孩子呢? } else {flag=0;} } return bt; } void Visit(BiTree bt) { while(bt->data!='$') printf("%c,",bt->data); } void PreOrder(BiTree bt) { if(bt!=NULL) { Visit(bt); PreOrder(bt->Lchild);//全部遍历 PreOrder(bt->Rchild);//全部遍历 } } int main() { BiTNode *bt; CreateBiTree(bt); PreOrder(bt); getch(); }
解决方案
数据结构-非递归遍历二叉树
数据结构-递归遍历二叉树
数据结构--第六章 遍历二叉树
解决方案二:
bt-Rchild=(BiTNode *)malloc(sizeof(bt-Rchild));
你有的没有写指针的访问符!
bt->Rchild
时间: 2024-08-31 18:24:12