首页 > 其他 > 详细

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

时间:2020-12-17 15:32:20      阅读:31      评论:0      收藏:0      [点我收藏+]

 

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;
}

 

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

原文:https://www.cnblogs.com/ganxiang/p/14149427.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!