【树】N叉树的最大深度

题目:

解答:

 1 /*
 2 // Definition for a Node.
 3 class Node {
 4 public:
 5     int val;
 6     vector<Node*> children;
 7 
 8     Node() {}
 9 
10     Node(int _val) {
11         val = _val;
12     }
13 
14     Node(int _val, vector<Node*> _children) {
15         val = _val;
16         children = _children;
17     }
18 };
19 */
20 
21 class Solution {
22 public:
23     
24     int maxDepth(struct Node* root)
25     {
26         if(!root) 
27         {
28             return 0;
29         }
30 
31         int max = 0;
32         for(int i = 0; i < root->children.size(); i++)
33         {
34             int temp = maxDepth(root->children[i]);
35             max = max > temp ? max : temp;
36         }
37         return max + 1;
38     }
39 };

猜你喜欢

转载自www.cnblogs.com/ocpc/p/12821697.html
今日推荐