Comments (2)
data.json
{
"code": 1,
"children": [
{
"code": 2,
"children": [
{
"code": 5,
"children": [
{
"code": 7
},
{
"code": 8
}
]
}
]
},
{
"code": 3,
"children": [
{
"code": 12,
"children": [
{
"code": 13
},
{
"code": 15
}
]
},
{
"code": 90,
"children": [
{
"code": 99
},
{
"code": 91
},
{
"code": 78
},
{
"code": 66
},
{
"code": 16,
"children": [
{
"code": 74,
"children": [
{
"code": 73,
"children": [
{
"code": 70,
"children": [
{
"code": 61
},
{
"code": 62
},
{
"code": 63,
"children": [
{
"code": 888
},
{
"code": 999,
"children": [
{
"code": 777
},
{
"code": 222
},
{
"code": 111
}
]
},
{
"code": 555
}
]
}
]
}
]
}
]
}
]
}
]
}
]
},
{
"code": 4,
"children": [
{
"code": 21,
"children": [
{
"code": 26
},
{
"code": 95
},
{
"code": 75
}
]
},
{
"code": 36,
"children": [
{
"code": 38,
"children": [
{
"code": 39
}
]
}
]
}
]
}
]
}
index.js
const data = require('./data.json')
function isChildren(node){
return node.children?true:false
}
var pathCode = function(node, code,key) {
const pathes = [];
_pathCode(node, code, pathes, [],key);
return pathes;
};
function _pathCode(node, code, pathes, path,key) {
path = [...path, node[key]];
if (node[key]===code){
pathes.push(...path)
return
}
if (isChildren(node)){
for (let i=0;i<node.children.length;i++){
_pathCode(node.children[i],code,pathes,path,key)
}
}
}
const paths = pathCode(data,555,'code')
from leetcode-javascript.
大佬的思路还是很明确的,一看就懂,但是 sumVals 这个函数对于节点较多时候还是比较耗时的,可以优化一下:
var pathSum = function (root, targetSum) {
const res = [];
helper(root, [], 0, targetSum, res);
return res;
};
var helper = function (node, paths, sum, targetSum, res) {
if (!node) return;
paths.push(node.val);
sum += node.val;
if (node.left) helper(node.left, paths, sum, targetSum, res);
if (node.right) helper(node.right, paths, sum, targetSum, res);
if (!node.left && !node.right) {
if (sum === targetSum) {
res.push(paths.slice());
}
}
// 退出当前递归,一定要将这次递归的节点去掉,因为要回到父节点重新开启另一条路径。
paths.pop();
sum -= node.val;
}
from leetcode-javascript.
Related Issues (20)
- 最大正方形-221
- 跳跃游戏 III-1306
- 跳跃游戏 IV-1345 HOT 1
- 有重复字符串的排列组合-面试题 08.08
- 黄金矿工-1219 HOT 1
- 字母大小写全排列-784 HOT 1
- 不同路径 III-980 HOT 1
- 下降路径最小和-931 HOT 3
- 找不同-389 HOT 6
- 和为K的子数组-560 HOT 2
- 矩阵置零-73 HOT 4
- 螺旋矩阵-54 HOT 1
- 螺旋矩阵 II-59 HOT 3
- 最长重复子数组-718
- 最接近的三数之和-16
- 顺次数-1291 HOT 7
- 最长的斐波那契子序列的长度-873 HOT 2
- 跳水板-面试题 16.11 HOT 2
- 编辑距离-72
- 计算方法
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from leetcode-javascript.