Fork me on GitHub

Leetcode559.Maximum Depth of N-ary TreeN叉树的最大深度

给定一个 N 叉树,找到其最大深度。

最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。

 

说明:

  1. 树的深度不会超过 1000。
  2. 树的节点总不会超过 5000。
class Solution {
public:
    int maxDepth(Node* root) {
        if(root == NULL)
            return 0;
        int cnt = 0;
        queue<Node*> q;
        q.push(root);
        while(!q.empty())
        {
            int s = q.size();
            cnt++;
            while(s--)
            {
                Node* node = q.front();
                q.pop();
                for(int i = 0; i < node ->children.size(); i++)
                {
                    if(node ->children[i] != NULL)
                    {
                        q.push(node ->children[i]);
                    }
                }
            }
        }
        return cnt;
    }
};

posted @ 2018-10-29 23:32  lMonster81  阅读(67)  评论(0编辑  收藏  举报
/*评论*/ /*top按钮*/

/* 网易云控件 */