From c39b92a633a0f04c517ef9912df2d7d3a71affc2 Mon Sep 17 00:00:00 2001 From: HermanZzz Date: Wed, 12 May 2021 15:59:24 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A00112.=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E6=80=BB=E5=92=8C=20JavaScript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0112.路径总和.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index 40df1e7a..51ee0872 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -312,6 +312,37 @@ Python: Go: +JavaScript: + +```javascript +/** + * @param {TreeNode} root + * @param {number} targetSum + * @return {boolean} + */ +let hasPathSum = function (root, targetSum) { + const traversal = (node, cnt) => { + // 遇到叶子节点,并且计数为0 + if (cnt === 0 && !node.left && !node.right) return true; + // 遇到叶子节点而没有找到合适的边(计数不为0),直接返回 + if (!node.left && !node.right) return false; + + // 左(空节点不遍历).遇到叶子节点返回true,则直接返回true + if (node.left && traversal(node.left, cnt - node.left.val)) return true; + // 右(空节点不遍历) + if (node.right && traversal(node.right, cnt - node.right.val)) return true; + return false; + }; + if (!root) return false; + return traversal(root, targetSum - root.val); + + // 精简代码: + // if (!root) return false; + // if (!root.left && !root.right && targetSum === root.val) return true; + // return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val); +}; +``` + From bb4f8273ede12e24498966d5e5a3dfc8188d6168 Mon Sep 17 00:00:00 2001 From: HermanZzz Date: Wed, 12 May 2021 16:01:18 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A00113.=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E6=80=BB=E5=92=8C-ii=20=E7=9A=84JavaScript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0112.路径总和.md | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) 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; +}; +``` +