-
Notifications
You must be signed in to change notification settings - Fork 0
/
0113.PathSumII.js
146 lines (105 loc) · 3.09 KB
/
0113.PathSumII.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Given the root of a binary tree and an integer targetSum, return all root-to-leaf paths where each path's sum equals targetSum.
// A leaf is a node with no children.
//
// Example 1:
// Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
// Output: [[5,4,11,2],[5,8,4,5]]
// Example 2:
// Input: root = [1,2,3], targetSum = 5
// Output: []
// Example 3:
// Input: root = [1,2], targetSum = 0
// Output: []
//
// Constraints:
// The number of nodes in the tree is in the range [0, 5000].
// -1000 <= Node.val <= 1000
// -1000 <= targetSum <= 1000
// 来源:力扣(LeetCode)
// 链接:https://leetcode-cn.com/problems/path-sum-ii
// 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
// 🎨 方法一:DFS
// 📝 思路:用两个队列分别存储节点和节点的路径
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
// 🎨 方法一:DFS
// 📝 思路:深度优先遍历时,存储路径
/**
* @param {TreeNode} root
* @param {number} targetSum
* @return {number[][]}
*/
var pathSum = function (root, targetSum) {
ans = [];
const path = []
dfs(root, targetSum, path);
return ans
};
function dfs(root, targetSum, path) {
if (root === null) {
return []
}
path.push(root.val)
if (root.left === null && root.right === null) {
const pathSum = path.reduce((acc, num) => acc + num, 0)
if (pathSum === targetSum) {
ans.push([...path]);
}
}
if (root.left) {
dfs(root.left, targetSum, path)
}
if (root.right) {
dfs(root.right, targetSum, path)
}
path.pop();
}
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
// 🎨 方法二:BFS
// 📝 思路:用两个队列分别存储节点和节点的路径
/**
* @param {TreeNode} root
* @param {number} targetSum
* @return {number[][]}
*/
var pathSum = function (root, targetSum) {
if (root === null) {
return []
}
const queNode = [root];
const quePath = [[root.val]];
const ans = [];
while (queNode.length) {
let currNode = queNode.pop()
let currPath = quePath.pop();
if (currNode.left === null && currNode.right === null) {
const sum = currPath.reduce((acc, curr) => acc + curr, 0)
if (sum === targetSum) {
ans.push(currPath);
}
continue
}
if (currNode.left) {
queNode.push(currNode.left)
quePath.push([...currPath, currNode.left.val])
}
if (currNode.right) {
queNode.push(currNode.right)
quePath.push([...currPath, currNode.right.val])
}
}
return ans
};