LeetCode—559. Maximum Depth of N-ary Tree


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

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

    Node() {}

    Node(int _val, vector _children) {
        val = _val;
        children = _children;
    }
};
*/
class Solution {
public:
    void getDepth(Node* root,int &depth,int level){
        if(!root)
            return;
        level++;
        depth=depth>level?depth:level;
        for(int i=0;ichildren.size();i++)
            getDepth(root->children[i],depth,level);
    }
    
    int maxDepth(Node* root) {
        int max=0;
        getDepth(root,max,0);
        return max;
    }
};


发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注