leetcode111. 二叉树的最小深度

题目链接

https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/

个人题解

class Solution {
private:
int min_h;
void dfs(TreeNode * root, int h){
    if (root==nullptr)return;
    dfs(root->left,h+1);
    if(root->left==nullptr && root->right==nullptr){
        min_h = min(min_h,h);
    }
    dfs(root->right,h+1);
}
public:
    int minDepth(TreeNode* root) {
        if(root==nullptr)return 0;
        min_h=1000000;
        dfs(root,1);
        return min_h;
    }
};
posted @ 2021-01-08 17:02  deepwzh  阅读(62)  评论(0编辑  收藏  举报