From dee25873fc8f594dbc0209453399669df5fdf61a Mon Sep 17 00:00:00 2001 From: Levi <3573897471@qq.com> Date: Fri, 7 Apr 2023 17:05:31 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=850111.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E5=85=B3=E4=BA=8E=E5=89=8D=E5=BA=8F=E9=81=8D=E5=8E=86?= =?UTF-8?q?=EF=BC=88=E4=B8=AD=E5=B7=A6=E5=8F=B3=EF=BC=89=E7=9A=84=E4=B8=AD?= =?UTF-8?q?=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0111.二叉树的最小深度.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/problems/0111.二叉树的最小深度.md b/problems/0111.二叉树的最小深度.md index de36c6f2..47569b05 100644 --- a/problems/0111.二叉树的最小深度.md +++ b/problems/0111.二叉树的最小深度.md @@ -170,11 +170,14 @@ class Solution { private: int result; void getdepth(TreeNode* node, int depth) { - if (node->left == NULL && node->right == NULL) { - result = min(depth, result); + // 函数递归终止条件 + if (root == nullptr) { return; } - // 中 只不过中没有处理的逻辑 + // 中,处理逻辑:判断是不是叶子结点 + if (root -> left == nullptr && root->right == nullptr) { + res = min(res, depth); + } if (node->left) { // 左 getdepth(node->left, depth + 1); } @@ -186,7 +189,9 @@ private: public: int minDepth(TreeNode* root) { - if (root == NULL) return 0; + if (root == nullptr) { + return 0; + } result = INT_MAX; getdepth(root, 1); return result;