Minimum Depth of Binary Tree LeetCode solution in C++

1
class Solution {
2
public:
3
void maxlevel(TreeNode *root, int level, int &ans) {
4
if (!root) return;
5

6
if (!root->left && !root->right) {
7
ans = min(level, ans);
8
return;
9
}
10

11
maxlevel(root->left, level + 1, ans);
12
maxlevel(root->right, level + 1, ans);
13
}
14
int minDepth(TreeNode *root) {
15
if (!root) return 0;
16

17
int ans = INT_MAX;
18
maxlevel(root, 0, ans);
19
return ans + 1;
20
}
21
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0