111. Minimum Depth of Binary Tree

来源:互联网 发布:mac修改mysql配置文件 编辑:程序博客网 时间:2024/06/16 22:11

这里写图片描述
用DFS完成

    int minDepth(TreeNode* root) {        if(!root) return 0;        if(!root->left&&!root->right) return 1;        if(root->left==NULL) return 1+minDepth(root->right);        else if(root->right==NULL) return 1+minDepth(root->left);        else return 1+min(minDepth(root->left),minDepth(root->right));    }