559. Maximum Depth of N-ary Tree

Posted andrewghost

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了559. Maximum Depth of N-ary Tree相关的知识,希望对你有一定的参考价值。

/*
// 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) {
        int depth = 0;
        Node* firstNode = root;
        queue<Node*> que;
        if (root) {
            que.push(root);
        }
        while (!que.empty()) {
            Node* node = que.front();
            que.pop();
            if (node == firstNode) {
                depth++;
                firstNode = NULL;
            }
            
            int num = node->children.size();
            if (num > 0 && !firstNode) {
                firstNode = node->children.at(0);
            }
            
            for (vector<Node*>::iterator it = node->children.begin(); it != node->children.end(); it++) {
                que.push(*it);
            }
        }
        
        return depth;
    }
};

  

以上是关于559. Maximum Depth of N-ary Tree的主要内容,如果未能解决你的问题,请参考以下文章

559. Maximum Depth of N-ary Tree

559. Maximum Depth of N-ary Tree

[LeetCode] 559. Maximum Depth of N-ary Tree

559. Maximum Depth of N-ary Tree - LeetCode

559. Maximum Depth of N-ary Tree

LeetCode559. Maximum Depth of N-ary Tree