题目链接

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;
    }
};

相关文章:

  • 2020-06-15
  • 2021-11-01
  • 2021-11-13
  • 2021-06-15
  • 2021-06-10
  • 2021-06-08
  • 2022-02-23
猜你喜欢
  • 2021-05-03
  • 2022-12-23
  • 2022-12-23
  • 2022-02-17
  • 2022-12-23
相关资源
相似解决方案