看板 b99902HW 關於我們 聯絡資訊
喵~這次就幫大家寫 以後檢查code要嘗試自己寫呀@_@a... ----------------- #include<stdio.h> struct treenode{ int data; struct treenode *left,*right; }; /********************************* Put your code here. **********************************/ int print(struct treenode *root,int depth){ if( root==NULL )return 0; int sum=0; sum+=print( root->left , depth+1 ); printf("level:%-5d| %*s\n",depth,2*depth,"oo"); sum+=print( root->right, depth+1 ); return sum+depth; } void insert(struct treenode *root,struct treenode *ptr){ if( ptr->data < root->data ) root->left==NULL?root->left=ptr:insert(root->left,ptr); else root->right==NULL?root->right=ptr:insert(root->right,ptr); } #include<time.h> int main(){ srand(time(NULL)); int n=10,ary[1000],i; struct treenode *root=NULL,*ptr=NULL; for(i=0;i<n;++i)ary[i] = i; for(i=0;i<n;++i){ int a = rand()%n; int b = rand()%n; int tmp=ary[a]; ary[a]=ary[b]; ary[b]=tmp; } for(i=0;i<n;++i){ ptr = (struct treenode*)malloc(sizeof(struct treenode)); ptr->data = ary[i]; ptr->left = ptr->right =NULL; if( root==NULL )root = ptr; else insert( root, ptr ); } puts("Print the tree:"); printf("Real Level-Sum of this tree is %d.\n",print( root,1 )); printf("Your Level-Sum of this tree is %d.\n",level_sum(root)); return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.160.236.199
shik:謝謝胖胖郭Q//////Q 12/02 16:46
s864372002:胖郭Q//////Q 12/02 17:45
yuscvscv:胖胖郭Q//////Q 12/02 22:35
suhorng: 胖胖郭Q//////Q 12/04 19:16