Maximum Depth of N-ary Tree LeetCode solution in C++

1
class Solution {
2
public:
3
int maxDepth(Node *root) {
4
if (root == NULL) {
5
return 0;
6
}
7
int dep = 1, mx = INT_MIN;
8
helper(root, dep, mx);
9
return mx;
10
}
11

12
void helper(Node *root, int dep, int &mx) {
13
if (root->children.size() == 0) {
14
mx = max(mx, dep);
15
}
16
for (int i = 0; i < root->children.size(); i++) {
17
helper(root->children[i], dep + 1, mx);
18
}
19
}
20
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0