240
笔下虽有千言,胸中实无一策

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;
    }
};
posted @ 2020-10-02 13:37  CasperWin  阅读(75)  评论(0编辑  收藏  举报