首页 > 其他 > 详细

559. Maximum Depth of N-ary Tree

时间:2019-02-02 15:31:41      阅读:176      评论:0      收藏:0      [点我收藏+]
/*
// 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

原文:https://www.cnblogs.com/AndrewGhost/p/10348517.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!