mirror of
https://github.com/arkorty/LeetCode.git
synced 2026-03-17 16:51:46 +00:00
39 lines
921 B
C++
39 lines
921 B
C++
#include <cstdint>
|
|
#include <iostream>
|
|
|
|
struct TreeNode {
|
|
int val;
|
|
TreeNode *left;
|
|
TreeNode *right;
|
|
TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
|
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
|
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
|
|
};
|
|
|
|
class Solution {
|
|
public:
|
|
void traverse(TreeNode *root, int curlvl, int &minCount) {
|
|
if (root == nullptr) {
|
|
return;
|
|
}
|
|
|
|
if (root->left == nullptr && root->right == nullptr && curlvl < minCount) {
|
|
minCount = curlvl;
|
|
}
|
|
|
|
traverse(root->left, curlvl + 1, minCount);
|
|
traverse(root->right, curlvl + 1, minCount);
|
|
}
|
|
|
|
int minDepth(TreeNode *root) {
|
|
if (root == nullptr) {
|
|
return 0;
|
|
}
|
|
|
|
int minCount = INT32_MAX;
|
|
traverse(root, 1, minCount);
|
|
|
|
return minCount;
|
|
}
|
|
};
|