#include<stdio.h>
#include<stdlib.h>
typedef struct BNode{//创建结构体
struct BNode*lnode,*rnode;
int data;
}BNode,*BTree;
BTree create();
void PreOrder();
int main(){
BTree root = create(3);
PreOrder(root);
}
BTree create(int high){//创建二叉树
if(high<=0){return NULL;}
BTree root = (BTree)malloc(sizeof(BNode));
root->data=rand()%100;
root->lnode=create(high-1);
root->rnode=create(high-1);
return root;
}
void PreOrder(BTree root){//对创建的二叉树先序遍历
if(root==NULL){return NULL;}
printf("%d\n",root->data);
PreOrder(root->lnode);
PreOrder(root->rnode);
}
使用递归创建二叉树并先序遍历
于 2022-12-19 21:32:25 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)