-
Notifications
You must be signed in to change notification settings - Fork 0
/
path-sum.js
48 lines (41 loc) · 1.17 KB
/
path-sum.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
/**
* 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)
* }
*/
/**
* 路径总和
* @param {TreeNode} root
* @param {number} targetSum
* @return {boolean}
*/
var hasPathSum = function (root, targetSum) {
// DFS (简洁清晰)
if (!root) return false;
// leaf node
if (!root.left && !root.right && targetSum === root.val) {
return true
}
let left = hasPathSum(root.left, targetSum - root.val);
let right = hasPathSum(root.right, targetSum - root.val);
return left || right;
// 迭代
// return preOrder(root, targetSum);
};
function preOrder(root, targetSum) {
const stack = [];
root && stack.push({ node: root, sum: targetSum });
while (stack.length) {
const curr = stack.pop();
const node = curr.node;
if (!node.left && !node.right && curr.sum === node.val) {
return true;
}
node.right && stack.push({ node: node.right, sum: curr.sum - node.val });
node.left && stack.push({ node: node.left, sum: curr.sum - node.val });
}
return false;
}