classSolution { public: intminDepth(TreeNode* root){ int depth = 0; queue<TreeNode*> qe; if (root) qe.push(root); while (!qe.empty()) { int node_num = qe.size(); for (int i = 0; i < node_num; i++) { auto tmp = qe.front(); qe.pop();
// 如果没有左右子树,则该节点为叶子节点,为最短深度 if (!tmp->left && !tmp->right) return depth + 1; else { if (tmp->left) qe.push(tmp->left); if (tmp->right) qe.push(tmp->right); } } ++depth; }