LeetCode——剑指 Offer 32 - II. 从上到下打印二叉树 II

一、题目

此题和剑指 Offer 32 - I为同类型的题目。可以先看前面的题目再来看这一题
从上到下按层打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行。
例如:
给定二叉树: [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回其层次遍历结果:

[
  [3],
  [9,20],
  [15,7]
]

二、思路

层次遍历,与上一个题目不同的是,这次要求输出的为二维数组,思路和上一个题目是一样的。这里采用两个辅助队列,第一个队列的元素的孩子输出到第二个队列,当第一个队列输出完毕后,开始输出第二个队列的元素,并且孩子全部输出到第一个队列,如此循环直到两个队列全部输出完成。

三、C语言解法

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
 
//返回二叉树节点数
 int calcTreeSize(struct TreeNode* root) 
{
    if(root == NULL)
    {
        return 0;
    }
    return 1 + calcTreeSize(root->left) + calcTreeSize(root->right);
} 
/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** levelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes){
    if(root == NULL){*returnSize =0;return NULL;}

        int count = 0,countCol=0;

        int QueenFront1=0,QueenSize1 = 0;
        int QueenFront2=0,QueenSize2 = 0;
        
        int retSize = calcTreeSize(root);

        int * countColSize=(int*) malloc(retSize*sizeof(int));

        struct TreeNode *tree1 = (struct TreeNode*)malloc(retSize*sizeof(struct TreeNode));//队列1
        struct TreeNode *tree2 = (struct TreeNode*)malloc(retSize*sizeof(struct TreeNode));//队列2

        int **ans = (int**) malloc(retSize*sizeof(int*));//行数组指针
        int *ansrow = (int*) malloc(retSize*retSize*sizeof(int));//每一行的数组指针

        for (int i = 0; i < retSize; i++) {
		ans[i] = ansrow + retSize*i;
	    }

        tree1[QueenFront1]= *root;
        QueenSize1++;
        while(QueenFront1 != QueenSize1||QueenFront2 != QueenSize2){
            while(QueenFront1 != QueenSize1) {
                ans[count][countCol]=tree1[QueenFront1].val;
                // printf("%d",ansrow[countCol]);
                countCol++;
                if(tree1[QueenFront1].left!=NULL)tree2[QueenSize2++] = *tree1[QueenFront1].left;
                if(tree1[QueenFront1].right!=NULL)tree2[QueenSize2++] = *tree1[QueenFront1].right;
                QueenFront1++;
            }

            if(countCol!=0){
                countColSize[count]=countCol;
                count++;
                countCol = 0;
            }

           while(QueenFront2 != QueenSize2) {
                ans[count][countCol]=tree2[QueenFront2].val;
                countCol++;
                if(tree2[QueenFront2].left!=NULL)tree1[QueenSize1++] = *tree2[QueenFront2].left;
                if(tree2[QueenFront2].right!=NULL)tree1[QueenSize1++] = *tree2[QueenFront2].right;
                QueenFront2++;
            }

            if(countCol!=0){
                countColSize[count]=countCol;
                count++;
                countCol = 0;
            }
            
        }
        *returnSize = count;
        *returnColumnSizes = countColSize;
        return ans;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天地神仙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值