/**
* Definition for a binary tree node.
* 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:
int r=INT_MAX;
void dfs(TreeNode* root,int h)
{
//h>=r剪枝
if(root==nullptr || h>=r) return;
if(root->left==nullptr && root->right==nullptr) r=min(r,h);
dfs(root->left,h+1);
dfs(root->right,h+1);
}
int minDepth(TreeNode* root) {
if(root==nullptr) return 0;
dfs(root,1);
return r;
}
};
09-07
193
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)