From fe6ae3c8a2cf5883b7ca94655057828794c89771 Mon Sep 17 00:00:00 2001 From: WanZhan <54505251+WanZhan-lucky@users.noreply.github.com> Date: Mon, 10 Feb 2025 21:21:02 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Update=200112.=E8=B7=AF=E5=BE=84=E6=80=BB?= =?UTF-8?q?=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 针对卡哥写的C++递归回溯,卡哥写的回溯有点绕,前面又return true,后面最终为return false。卡哥这个地方也没讲清楚,一笔带过了,所以我结合之前的对称二叉树,直接return leftpath || rightpath;对左右子树取或操作,更好理解,我认为 --- ...57\345\276\204\346\200\273\345\222\214.md" | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" index b97013e67a..e40d86fcab 100644 --- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" +++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" @@ -133,17 +133,29 @@ private: if (!cur->left && !cur->right && count == 0) return true; // 遇到叶子节点,并且计数为0 if (!cur->left && !cur->right) return false; // 遇到叶子节点直接返回 - if (cur->left) { // 左 - count -= cur->left->val; // 递归,处理节点; - if (traversal(cur->left, count)) return true; - count += cur->left->val; // 回溯,撤销处理结果 + bool leftpath = false, rightpath = false; + if (node->left) { + leftpath = pathSum(node->left, count - node->left->val); + // if (leftpath) return true; } - if (cur->right) { // 右 - count -= cur->right->val; // 递归,处理节点; - if (traversal(cur->right, count)) return true; - count += cur->right->val; // 回溯,撤销处理结果 + if (node->right) { + rightpath = pathSum(node->right, count - node->right->val); + // if (rightpath) return true; } - return false; + return leftpath || rightpath; + + // if (cur->left) { // 左 + // count -= cur->left->val; // 递归,处理节点; + // if (traversal(cur->left, count)) return true; + // count += cur->left->val; // 回溯,撤销处理结果 + // } + + // if (cur->right) { // 右 + // count -= cur->right->val; // 递归,处理节点; + // if (traversal(cur->right, count)) return true; + // count += cur->right->val; // 回溯,撤销处理结果 + // } + // return false; } public: From 002f9b1f0f7baf7f03e1bfb12c2d5116521cc3ea Mon Sep 17 00:00:00 2001 From: WanZhan <54505251+WanZhan-lucky@users.noreply.github.com> Date: Mon, 10 Feb 2025 21:27:41 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Update=200112.=E8=B7=AF=E5=BE=84=E6=80=BB?= =?UTF-8?q?=E5=92=8C.md=20=E7=AE=80=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 把递归的写法简化,卡哥写的有点绕,即使是最后精简的, if里面又套if,中间又返回return true,最后return false,我学这道题看了蛮久,我改进了一点,感觉更好理解一些,直接return leftpath || rightpath --- ...67\257\345\276\204\346\200\273\345\222\214.md" | 15 --------------- 1 file changed, 15 deletions(-) diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" index e40d86fcab..63ec043b53 100644 --- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" +++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" @@ -136,26 +136,11 @@ private: bool leftpath = false, rightpath = false; if (node->left) { leftpath = pathSum(node->left, count - node->left->val); - // if (leftpath) return true; } if (node->right) { rightpath = pathSum(node->right, count - node->right->val); - // if (rightpath) return true; } return leftpath || rightpath; - - // if (cur->left) { // 左 - // count -= cur->left->val; // 递归,处理节点; - // if (traversal(cur->left, count)) return true; - // count += cur->left->val; // 回溯,撤销处理结果 - // } - - // if (cur->right) { // 右 - // count -= cur->right->val; // 递归,处理节点; - // if (traversal(cur->right, count)) return true; - // count += cur->right->val; // 回溯,撤销处理结果 - // } - // return false; } public: