创建一颗二叉树

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
struct node{
	int date;
	node *lchild;
	node *rchild;
	node(){
		lchild=rchild=NULL;
	}	
};
int a[11]={0,1,2,3,4,5,6,7,8,9,10};
void create(node *&root,int x)
{
	if(x>10)
	return ;
	
	root=new node();	
	root->date =a[x];
	
	create(root->lchild,2*x);
	create(root->rchild,2*x+1); 
}
void pre(node *root)
	{
		if(root)
		{
			cout<<root->date<<endl;
			pre(root->lchild);
			pre(root->rchild);
			
		}
	}
int main()
{
	node *root;
	create(root,1);
	pre(root);	
} 
发布了31 篇原创文章 · 获赞 13 · 访问量 737

猜你喜欢

转载自blog.csdn.net/weixin_43310882/article/details/103336319