创建简单的二叉树

#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
 struct Snode *lchild;
 struct Snode *rchild;
 int  data;
}*Node,Snode;
void create(Node *l){
 int c;
 scanf("%d",&c);//若是使用字符类型则会跳转,因为将enter也吸收进缓冲区
 if(c==0){
  *l = NULL;//子叶
 }
 else{
  *l = (Snode *)malloc(sizeof(Snode));
  (*l)->data = c;
  create(&((*l)->lchild));
  create(&((*l)->rchild));
 }
}
void print(Node *l){
 int c;
 if(*l){
  c = (*l)->data;
  printf("%d",c);
  print(&(*l)->lchild);
  print(&(*l)->rchild);
 }
}
void main(){
 Node l = NULL;
 create(&l);
 print(&l);
}

猜你喜欢

转载自www.cnblogs.com/cat-cat/p/9942299.html