LeetCode-103-二叉树的锯齿形层次遍历-C语言

/*
* 算法思想:
* 与102题目一样,102的结果,第i&1 != 0 行,反转即可。
*
*
*/

#define LEV 1024
#define LEN 1024

typedef struct  TreeNode  Node;

void get(Node *node, int level, int **ret, int *ret_index, int *rcs){
    if(!node) return;
    
    if(!ret[level]){
        ret[level] = (int *)malloc(sizeof(int) * LEN);
        rcs[level] = 0;
    }
    
    if(level > *ret_index){
        *ret_index = level;
    }
    
     ret[level][rcs[level]++] = node->val; 
    
    get(node->left, level+1, ret, ret_index, rcs);
  
     get(node->right, level+1, ret, ret_index, rcs);
  
    
}

int** zigzagLevelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes){
    int **ret = (int **)malloc(sizeof(int*) * LEV);
    int *rcs = (int*)malloc(sizeof(int) * LEV);
    int ret_index = 0;
    int i, j, tmp, k;
    
    memset(ret, 0, sizeof(int*) * LEV);
    memset(rcs, 0, sizeof(int) * LEV);
    
    get(root, 0, ret, &ret_index, rcs);
    
    *returnSize = root==NULL ? 0 : ret_index+1;
    *returnColumnSizes = rcs;
    
    for(i=0; i<*returnSize; i++){
        if(i&1) {
            k = rcs[i]-1; j=0;
            while(j

你可能感兴趣的:(LeetCode)