TreeNode *sortArr(vector<int> &num, int begin,int end) // passing reference of num instead of value, or the recursion will waste lots of memory. { TreeNode *p = NULL; if ( begin > end) { return p; } int mid = (begin+end) / 2; p = new TreeNode(num[mid]); p->left = sortArr(num,begin,mid-1); p->right = sortArr(num,mid+1,end); return p; }
TreeNode *sortArr(vector<int> num, int begin,int end) { TreeNode *p = NULL; if ( begin > end) { return p; } int mid = (begin+end) / 2; p = new TreeNode(num[mid]); p->left = sortArr(num,begin,mid-1); p->right = sortArr(num,mid+1,end); return p; }
TreeNode *sortedArrayToBST(vector<int> &num) { int len = num.size(); if (len == 0) { return NULL; } return sortArr(num,0,len-1); }
int main() { char *a[] = {"abcd","bbb","ccc","2222","eeee"}; char b[4][10] = {"3333","34444","hhhhh","qqqqq"}; char **arr = NULL; //printf("%d", sizeof(a) / sizeof(char *)); //printf("%d",sizeof(*b)); sortArr(a,sizeof(a)/sizeof(char *),b,sizeof(b)/sizeof(*b),&arr); printArr(arr,9); system("pause"); }
void main03() { Teacher Array[3]; int i=0; int num=3; for(i=0;i<num;i++){ printf("\n enter your age:"); scanf("%d",&(Array[i].age)); } for(i=0;i<num;i++){ printf("age:%d\n",Array[i].age); } printf("print===============\n"); printArr(Array,num);//数组首元素代表地址 sortArr(&Array,num); printf("print===============\n"); printArr(Array,num);//数组首元素代表地址 printf("%s\n","hello,world!"); system("pause"); }