Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int maxwidth(struct TreeNode* root) {
- if (!root) {
- return 0;
- }
- int maxWidth = 0;
- struct TreeNode** queue = (struct TreeNode**) malloc(sizeof(struct TreeNode*) * 1000);
- int front = 0, rear = 0;
- queue[rear++] = root;
- while (front != rear) {
- int size = rear - front;
- int leftmost = 0, rightmost = 0;
- for (int i = 0; i < size; i++) {
- struct TreeNode* node = queue[front++];
- if (i == 0) {
- leftmost = node->val;
- }
- if (i == size - 1) {
- rightmost = node->val;
- }
- if (node->left) {
- node->left->val = node->val * 2;
- queue[rear++] = node->left;
- }
- if (node->right) {
- node->right->val = node->val * 2 + 1;
- queue[rear++] = node->right;
- }
- }
- int width = rightmost - leftmost + 1;
- maxWidth = maxWidth < width ? width : maxWidth;
- }
- free(queue);
- return maxWidth;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement