diff --git "a/2101040022/chapter_7/\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\345\256\275\345\272\246.cpp" "b/2101040022/chapter_7/\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\345\256\275\345\272\246.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..0b435b1031e8abc8d109c664a951b4044516f886 --- /dev/null +++ "b/2101040022/chapter_7/\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\345\256\275\345\272\246.cpp" @@ -0,0 +1,38 @@ +#define MAX_NODE_SIZE 3000 +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +typedef struct { + struct TreeNode *node; + unsigned long long index; +} Pair; + +int widthOfBinaryTree(struct TreeNode* root){ + unsigned long long res = 1; + Pair * arr = (Pair *)malloc(sizeof(Pair) * MAX_NODE_SIZE); + Pair * tmp = (Pair *)malloc(sizeof(Pair) * MAX_NODE_SIZE); + int arrSize = 0, tmpSize = 0; + arr[arrSize].node = root; + arr[arrSize].index = 1LL; + arrSize++; + while (arrSize > 0) { + tmpSize = 0; + for (int i = 0; i < arrSize; i++) { + if (arr[i].node->left) { + tmp[tmpSize].node = arr[i].node->left; + tmp[tmpSize].index = arr[i].index * 2; + tmpSize++; + } + if (arr[i].node->right) { + tmp[tmpSize].node = arr[i].node->right; + tmp[tmpSize].index = arr[i].index * 2 + 1; + tmpSize++; + } + } + res = MAX(res, arr[arrSize - 1].index - arr[0].index + 1); + arrSize = tmpSize; + Pair * p = arr; + arr = tmp; + tmp = p; + } + return res; +} \ No newline at end of file