class Solution {
public:
    int minDepth(TreeNode* root) {
        if (!root) return 0;
        if (!root->left) return 1 + minDepth(root->right);
        if (!root->right) return 1 + minDepth(root->left);
        return 1 + min(minDepth(root->left), minDepth(root->right));
    }
};
class Solution {
public:
    int minDepth(TreeNode* root) {
        if (!root) return 0;
        int res = 0;
        queue<TreeNode*> q{{root}};
        while (!q.empty()) {
            ++res;
            for (int i = q.size(); i > 0; --i) {
                auto t = q.front(); q.pop();
                if (!t->left && !t->right) return res;
                if (t->left) q.push(t->left);
                if (t->right) q.push(t->right);
            }
        }
        return -1;
    }
};

相关文章:

  • 2021-11-02
  • 2021-06-09
  • 2022-12-23
  • 2022-12-23
  • 2021-09-23
  • 2021-08-14
  • 2021-05-22
猜你喜欢
  • 2021-09-17
  • 2022-12-23
  • 2021-12-21
  • 2021-04-17
  • 2021-08-21
相关资源
相似解决方案