#10422. szjy_6_树_042
szjy_6_树_042
szjy_6_树_042
阅读代码,程序输出是( )。
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
struct Node {
int value;
Node* left = nullptr;
Node* right = nullptr;
};
int maxWidth(Node* root) {
queue<Node*> q;
q.push(root);
int ans = 0;
while (!q.empty()) {
int cnt = q.size();
ans = max(ans, cnt);
while (cnt--) {
Node* p = q.front(); q.pop();
if (p->left) q.push(p->left);
if (p->right) q.push(p->right);
}
}
return ans;
}
int main() {
Node a{1}, b{2}, c{3}, d{4}, e{5}, f{6};
a.left = &b; a.right = &c;
b.left = &d; b.right = &e;
c.right = &f;
cout << maxWidth(&a);
}
{{ select(1) }}
- 2
- 3
- 4
- 6