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..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" @@ -133,17 +133,14 @@ 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 (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); } - return false; + return leftpath || rightpath; } public: