559. Maximum Depth of N-ary Tree C++N叉树的最大深度

网址:https://leetcode.com/problems/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) {
        if(!root)
            return 0;
        int res = 1;
        for(Node* child : root->children)
            res = max(res,maxDepth(child)+1);
        return res;
    }
};

原文地址:https://www.cnblogs.com/tornado549/p/10073608.html