zoukankan      html  css  js  c++  java
  • 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;
        }
    };
    

      

  • 相关阅读:
    构造并判断二叉搜索树-js
    构造树并判断是否对称
    旋转图像
    螺旋矩阵
    链表实现快速排序
    任务调度器
    队列的实现
    最大矩形
    棒球比赛
    复原IP地址
  • 原文地址:https://www.cnblogs.com/AndrewGhost/p/10348517.html
Copyright © 2011-2022 走看看