码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 559. Maximum Depth of N-ary Tree

时间:2020-02-07 20:43:21      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:最大   color   mic   src   etc   max   def   init   efi   

技术图片

 

 

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

标签:最大   color   mic   src   etc   max   def   init   efi   

原文地址:https://www.cnblogs.com/littlepage/p/12274253.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!