Leetcode 559. Maximum Depth of N-ary Tree

Posted Steve Yu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 559. Maximum Depth of N-ary Tree相关的知识,希望对你有一定的参考价值。

 

 

c++,如果本节点为空,返回0,否则返回 这棵树孩子中(找到每个节点的最大值,返回最大值+1即可,1是本节点的深度)

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

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/
class Solution {
public:
    int maxDepth(Node* root) {
        if(!root) return 0;
        int ans = 0;
        for(int i = 0; i < root->children.size(); i++)
            ans = max(ans, maxDepth(root->children[i]));
        return ans + 1;
    }
};

 

以上是关于Leetcode 559. Maximum Depth of N-ary Tree的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode559. Maximum Depth of N-ary Tree

[LeetCode&Python] Problem 559. Maximum Depth of N-ary Tree

Leetcode 559. Maximum Depth of N-ary Tree

559. Maximum Depth of N-ary Tree

559. Maximum Depth of N-ary Tree

559. N 叉树的最大深度简单