diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index 51ee0872..718a2f5b 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -314,6 +314,8 @@ Go: JavaScript: +0112.路径总和 + ```javascript /** * @param {TreeNode} root @@ -321,6 +323,7 @@ JavaScript: * @return {boolean} */ let hasPathSum = function (root, targetSum) { + // 递归法 const traversal = (node, cnt) => { // 遇到叶子节点,并且计数为0 if (cnt === 0 && !node.left && !node.right) return true; @@ -343,6 +346,40 @@ let hasPathSum = function (root, targetSum) { }; ``` +0113.路径总和-ii + +```javascript +let pathSum = function (root, targetSum) { + // 递归法 + // 要遍历整个树找到所有路径,所以递归函数不需要返回值, 与112不同 + const res = []; + const travelsal = (node, cnt, path) => { + // 遇到了叶子节点且找到了和为sum的路径 + if (cnt === 0 && !node.left && !node.right) { + res.push([...path]); // 不能写res.push(path), 要深拷贝 + return; + } + if (!node.left && !node.right) return; // 遇到叶子节点而没有找到合适的边,直接返回 + // 左 (空节点不遍历) + if (node.left) { + path.push(node.left.val); + travelsal(node.left, cnt - node.left.val, path); // 递归 + path.pop(); // 回溯 + } + // 右 (空节点不遍历) + if (node.right) { + path.push(node.right.val); + travelsal(node.right, cnt - node.right.val, path); // 递归 + path.pop(); // 回溯 + } + return; + }; + if (!root) return res; + travelsal(root, targetSum - root.val, [root.val]); // 把根节点放进路径 + return res; +}; +``` +