#include <stdio.h>
#include <malloc.h>
typedef struct BTreeNode{
int value;
BTreeNode * lchild;
BTreeNode * rchild;
}BTreeNode;
void visit(BTreeNode * node){
printf("%d ",node->value);
}
void preOrder(BTreeNode * root){
if(root){
visit(root);
}
if(root->lchild != NULL){
preOrder(root->lchild);
}
if(root->rchild != NULL){
preOrder(root->rchild);
}
}
BTreeNode * seqToBTree(int *seq,int start,int end){
if(start > end){
return NULL;
}
int mid = start + (end -start)/2;
BTreeNode * root = (BTreeNode *)malloc(sizeof(BTreeNode));
root->value = seq[mid];
root->lchild = seqToBTree(seq,start,mid - 1);
root->rchild = seqToBTree(seq,mid + 1,end);
return root;
}
main(){
int a[] = {1,2,3,4,5,6,7,8,9};
BTreeNode * root = seqToBTree(a,0,8);
preOrder(root);
system("pause");
return 0;
}
【学习点滴-数据结构-二叉树】整数序列放入二叉树中
最新推荐文章于 2021-08-30 07:44:16 发布