问题
思路
相当于层次遍历,按照遍历二叉树的相应知识来
代码
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* Return an array of size *returnSize.
* Note: returned array must be malloced, assume caller calls free().
*/
int* printFromTopToBottom(struct TreeNode* root , int* returnSize){
int *res = (int*)malloc(sizeof(int)*1000),len = 0;
struct TreeNode *q[1000];
int front=0,rear=0;
q[rear++] = root;
while(front < rear){
if(q[front]!=NULL){
res[len++] = q[front]->val;
if(q[front]->left != NULL) q[rear++] = q[front]->left;
if(q[front]->right != NULL) q[rear++] = q[front]->right;
}
front++;
}
*returnSize = len;
return res;
}