Leetcode 774 Solution

This article provides solution to leetcode question 774 (maximum-depth-of-n-ary-tree)

https://leetcode.com/problems/maximum-depth-of-n-ary-tree

Solution

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/
class Solution {
public:
    int maxDepth(Node* root) {
        if (root == NULL)
            return 0;

        queue<Node*> q;
        q.push(root);

        int depth = 0;
        while (q.empty() == false)
        {
            depth++;

            int s = q.size();
            for (int i = 0; i < s; i++)
            {
                auto node = q.front();
                q.pop();

                for (auto child : node->children)
                    q.push(child);
            }
        }

        return depth;
    }
};