【leetcode】103. 二叉樹的鋸齒形層次遍歷
阿新 • • 發佈:2020-12-17
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; }