Merge pull request #2015 from L-evi/master

补充0111.二叉树的最小深度中的关于前序遍历(中左右)的中处理逻辑
This commit is contained in:
程序员Carl
2023-04-23 09:30:50 +08:00
committed by GitHub

View File

@ -170,11 +170,14 @@ class Solution {
private: private:
int result; int result;
void getdepth(TreeNode* node, int depth) { void getdepth(TreeNode* node, int depth) {
if (node->left == NULL && node->right == NULL) { // 函数递归终止条件
result = min(depth, result); if (root == nullptr) {
return; return;
} }
// 中 只不过中没有处理逻辑 // 中处理逻辑:判断是不是叶子结点
if (root -> left == nullptr && root->right == nullptr) {
res = min(res, depth);
}
if (node->left) { // 左 if (node->left) { // 左
getdepth(node->left, depth + 1); getdepth(node->left, depth + 1);
} }
@ -186,7 +189,9 @@ private:
public: public:
int minDepth(TreeNode* root) { int minDepth(TreeNode* root) {
if (root == NULL) return 0; if (root == nullptr) {
return 0;
}
result = INT_MAX; result = INT_MAX;
getdepth(root, 1); getdepth(root, 1);
return result; return result;