30 Day Challenge Day 17 | Leetcode 559. Maximum Depth of N-ary Tree

题解

Easy | BFS

典型的求最大深度,用BFS。

class Solution {
public:
    int maxDepth(Node* root) {
        if(!root) return 0;

        queue<Node*> q;
        q.push(root);
        
        int max_depth = 0;
        
        while(!q.empty()) {
            max_depth++;
            int sz = q.size();
            for(int i = 0; i <sz; i++) {
                auto t = q.front();
                q.pop();
                for(auto n : t->children) {
                    if(n) q.push(n);
                }
            }
        }
        
        return max_depth;
    }
};
原文地址:https://www.cnblogs.com/casperwin/p/13761135.html