mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
添加0113.路径总和-ii 的JavaScript版本
This commit is contained in:
@ -314,6 +314,8 @@ Go:
|
|||||||
|
|
||||||
JavaScript:
|
JavaScript:
|
||||||
|
|
||||||
|
0112.路径总和
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
/**
|
/**
|
||||||
* @param {TreeNode} root
|
* @param {TreeNode} root
|
||||||
@ -321,6 +323,7 @@ JavaScript:
|
|||||||
* @return {boolean}
|
* @return {boolean}
|
||||||
*/
|
*/
|
||||||
let hasPathSum = function (root, targetSum) {
|
let hasPathSum = function (root, targetSum) {
|
||||||
|
// 递归法
|
||||||
const traversal = (node, cnt) => {
|
const traversal = (node, cnt) => {
|
||||||
// 遇到叶子节点,并且计数为0
|
// 遇到叶子节点,并且计数为0
|
||||||
if (cnt === 0 && !node.left && !node.right) return true;
|
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;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user