Steward
分享是一種喜悅、更是一種幸福
程式語言 - LeetCode - C++ - 559. Maximum Depth of N-ary Tree
題目:

解答:
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val) {
val = _val;
}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
int maxDepth(Node* root) {
int ans = 0;
queue<Node*> q;
if (!root) {
return ans;
}
q.push(root);
while (!q.empty()) {
int size = q.size();
for (int i = 0; i < size; ++i) {
Node *n = q.front();
q.pop();
for (auto &c : n->children) {
q.push(c);
}
}
ans += 1;
}
return ans;
}
};