NO.103 二叉树的锯齿形层次遍历

给定一个二叉树,返回其节点值的锯齿形层次遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。

例如:
给定二叉树 [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回锯齿形层次遍历如下:

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

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *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** zigzagLevelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes){
    *returnSize=0;
    struct TreeNode** save=NULL;
    int ** ret=NULL;
    *returnColumnSizes=NULL;
    if(root)
    {
        *returnSize=1;
        *returnColumnSizes=(int *)malloc(sizeof(int));
        (*returnColumnSizes)[0]=1;
        ret=(int**)realloc(ret,sizeof(int *));
        ret[0]=(int *)malloc(sizeof(int));
        ret[0][0]=root->val;
        save=(struct TreeNode**)realloc(save,sizeof(struct TreeNode*));
        save[0]=root;
        while(1)
        {
            struct TreeNode** tmp=NULL;
            int size=0;
            int *num=NULL;
            for(int i=(*returnColumnSizes)[(*returnSize)-1]-1;i>=0;i--)
            {
                if(*returnSize %2)
                {
                    if(save[i]->right)
                    {
                        size++;
                        tmp=(struct TreeNode**)realloc(tmp,sizeof(struct TreeNode*)*size);
                        num=(int*)realloc(num,sizeof(int)*size);
                        tmp[size-1]=save[i]->right;
                        num[size-1]=save[i]->right->val;
                    }
                    if(save[i]->left)
                    {
                        size++;
                        tmp=(struct TreeNode**)realloc(tmp,sizeof(struct TreeNode*)*size);
                        num=(int*)realloc(num,sizeof(int)*size);
                        tmp[size-1]=save[i]->left;
                        num[size-1]=save[i]->left->val;
                    }
                }
                else
                {
                    if(save[i]->left)
                    {
                        size++;
                        tmp=(struct TreeNode**)realloc(tmp,sizeof(struct TreeNode*)*size);
                        num=(int*)realloc(num,sizeof(int)*size);
                        tmp[size-1]=save[i]->left;
                        num[size-1]=save[i]->left->val;
                    }
                    if(save[i]->right)
                    {
                        size++;
                        tmp=(struct TreeNode**)realloc(tmp,sizeof(struct TreeNode*)*size);
                        num=(int*)realloc(num,sizeof(int)*size);
                        tmp[size-1]=save[i]->right;
                        num[size-1]=save[i]->right->val;
                    }
                }
            }
            if(size==0)break;
            else
            {
                (* returnSize)++;
                *returnColumnSizes=(int *)realloc(*returnColumnSizes,sizeof(int)*(* returnSize));
                (*returnColumnSizes)[(* returnSize)-1]=size;
                ret=(int**)realloc(ret,sizeof(int *)*(* returnSize));
                ret[(* returnSize)-1]=num;
                free(save);
                save=tmp;
            }
        }
    }
    free(save);
    return ret;
}

执行用时 :4 ms, 在所有C提交中击败了87.50% 的用户

内存消耗 :9.1 MB, 在所有C提交中击败了100.00%的用户

猜你喜欢

转载自blog.csdn.net/xuyuanwang19931014/article/details/91399648