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

一、题目

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

    3
   / \
  9  20
    /  \
   15   7

返回其层次遍历结果:

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

二、思路

大概思路查看上面提到的文章,在剑指 Offer 32 - II的答案上,将奇数行元素翻转即可

三、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 * countColSize=(int*) malloc(1010*sizeof(int));
        

        int QueenFront1=0,QueenSize1 = 0;
        int QueenFront2=0,QueenSize2 = 0;

        int retSize = calcTreeSize(root);

        struct TreeNode *tree1 = (struct TreeNode*)malloc((retSize+1)*sizeof(struct TreeNode));//队列1
        struct TreeNode *tree2 = (struct TreeNode*)malloc((retSize+1)*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;
            }
        }
        int left=0,right=0,temp=0;;
        for(int i=1;i<count;i+=2){
            left = 0;
            right = countColSize[i]-1;
            while(left<right){
                temp = ans[i][right];
                ans[i][right] = ans[i][left];
                ans[i][left] = temp;
                left++;
                right--;
            }
        }
        *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、付费专栏及课程。

余额充值