diff --git "a/2209040008/chapter7/\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.cpp" "b/2209040008/chapter7/\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..ba646e7c00320038f48580fbcab7bb82270a4ea7 --- /dev/null +++ "b/2209040008/chapter7/\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.cpp" @@ -0,0 +1,31 @@ +int** levelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes){ + int** ans=(int**)malloc(sizeof(int*)*2000); + *returnSize=0; + if(!root) + { + return NULL; + } + int columnSizes[2000]; + struct TreeNode* queue[2000];//模拟队列 + int rear=0;int head=0;//记录队列头尾 + queue[rear++]=root;//录入根结点 + + while(rear!=head) + { + ans[(*returnSize)]=(int*)malloc(sizeof(int)*(rear-head)); + columnSizes[(*returnSize)]=rear-head; + int start=head;//记录遍历开始位置,即为本层的头 + head=rear; + for(int i=start;ival; + if(queue[i]->left) queue[rear++]=queue[i]->left; + if(queue[i]->right) queue[rear++]=queue[i]->right;//rear不断改变 + } + (*returnSize)++;//一层遍历完*returnSize加一; + + } + *returnColumnSizes=(int*)malloc(sizeof(int)*(*returnSize));//给出*returnColumnSizes + for(int i=0;i<*returnSize;i++) (*returnColumnSizes)[i]=columnSizes[i]; + return ans; +} \ No newline at end of file