2012腾讯广研
把一个有序整数数组放到有序二叉树中。
#include <iostream>
using namespace std;
struct Tree
{
int value;
Tree *leftt;
Tree *rightt;
};
void createTree(int *num,int size,Tree *&myTree)
{
if(size>0)
{
myTree = new Tree;
int mid = size/2;
myTree->value = num[size/2];
myTree->leftt = NULL;
myTree->rightt = NULL;
createTree(num,mid,myTree->leftt);
createTree(num+mid+1,size-mid-1,myTree->rightt);
}
else
myTree = NULL;
}
int main()
{
int size;
int *num;
Tree *myTree;
while(cin>>size)
{
for(int i=0;i<size;i++){
cin>>num[i];//输入的是有序数组
}
createTree(num,size,myTree);
}
}