leetcode559+求N叉树的最大深度,递归和二叉树一样

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013554860/article/details/82156394

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

/*
// 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==NULL) return 0;
        int dep = 0;
        for(auto n: root->children){
            dep = max(dep, maxDepth(n));
        }
        return dep+1;
    }
};

阅读更多
换一批

没有更多推荐了,返回首页