559. Maximum Depth of N-ary Tree C++N叉树的最大深度

时间:2022-04-01 14:41:05

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

很简单的深度优先搜索

设定好递归返回的条件和值

/*
// 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)
return ;
int res = ;
for(Node* child : root->children)
res = max(res,maxDepth(child)+);
return res;
}
};

559. Maximum Depth of N-ary Tree C++N叉树的最大深度