LeetCode 559. N叉树的最大深度
思路:搜索
挨个搜索每个结点的孩子,返回孩子结点深度最大值 + 1 +1 +1
时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( n ) O(n) O(n)
/*
// 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 res = 0;
for (auto c : root->children) res = max(res, maxDepth(c));
return res + 1;
}
};