-
Notifications
You must be signed in to change notification settings - Fork 0
/
662.cpp
59 lines (49 loc) · 1.36 KB
/
662.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<vector>
#include<string>
#include<map>
#include<queue>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
int widthOfBinaryTree(TreeNode* root) {
if(!root)return 0;
int mi = INT32_MAX;
int ma = INT32_MIN;
int ret = 0;
root->val = 1;
queue<TreeNode*> q;
q.push(root);
q.push(NULL);
int level = 1;
while(!q.empty())
{
TreeNode* p = q.front();
q.pop();
if(p)
{
if(p->val < mi)mi = p->val;
if(p->val > ma)ma = p->val;
//靠,总是乘2超限了
if(p->left){p->left->val = 2 * p->val - (mi == INT32_MAX ? 0 : mi);q.push(p->left);}
if(p->right){p->right->val = 2 * p->val + 1 - (mi == INT32_MAX ? 0 : mi);q.push(p->right);}
}
//新一层级
//大哥你发没发现你这样的话是无限的了...
else
{
++level;
if(!q.empty())q.push(NULL);
if(ma - mi + 1> ret)ret = ma - mi + 1;
mi = INT32_MAX;
ma = INT32_MIN;
}
}
return ret;
}
};