【leetcode】103. 二叉树的锯齿形层次遍历

int** zigzagLevelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes){
    int** arr = (int**)calloc(100, sizeof(int*));
    *returnSize = 0;
    *returnColumnSizes = (int*)calloc(100, sizeof(int));
    if (!root) return arr;
    struct TreeNode* stArr[2000] = { 0 };
    int i, j, left = 0, right = 0;
    stArr[right++] = root;
    while (left < right){
        int len = right - left;
        arr[(*returnSize)] = (int*)calloc(len, sizeof(int));        
        for (i = left + len - 1; i >= left; i--)
        {
            arr[(*returnSize)][(*returnColumnSizes)[*returnSize]++] = stArr[i]->val;
            if ((*returnSize) % 2 == 0){
                if (stArr[i]->left)
                    stArr[right++] = stArr[i]->left;
                if (stArr[i]->right)
                    stArr[right++] = stArr[i]->right;
            }
            else{
                if (stArr[i]->right)
                    stArr[right++] = stArr[i]->right;
                if (stArr[i]->left)
                    stArr[right++] = stArr[i]->left;
            }
        }
        (*returnSize)++;
        left += len;
    }
    return arr;
}
原文地址:https://www.cnblogs.com/ganxiang/p/14149427.html