LeetCode---559. Maximum Depth of N-ary Tree

  • 内容
  • 评论
  • 相关
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//url: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:
    void getDepth(Node* root,int &depth,int level){
        if(!root)
            return;
        level++;
        depth=depth>level?depth:level;
        for(int i=0;i<root->children.size();i++)
            getDepth(root->children[i],depth,level);
    }
 
    int maxDepth(Node* root) {
        int max=0;
        getDepth(root,max,0);
        return max;
    }
};

原创文章,转载请注明: 转载自royalchen的博客

本文链接地址: LeetCode---559. Maximum Depth of N-ary Tree

评论

0条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注