题解 | #从上往下打印二叉树#
从上往下打印二叉树
http://www.nowcoder.com/practice/7fe2212963db4790b57431d9ed259701
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*
* C语言声明定义全局变量请加上static,防止重复定义
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型一维数组
* @return int* returnSize 返回数组行数
*/
int *PrintFromTopToBottom(struct TreeNode *root, int *returnSize)
{
int *num, pos = 0;
num = (int *)malloc(sizeof(int) * 1001);
*returnSize = pos;
if (!root)
return num;
struct TreeNode *que[1000], *temp;
int front = 0, rear = 0;
que[++rear] = root;
while (rear != front)
{
temp = que[++front];
num[pos++] = temp->val;
if (temp->left)
que[++rear] = temp->left;
if (temp->right)
que[++rear] = temp->right;
}
*returnSize = pos;
return num;
}