-
Notifications
You must be signed in to change notification settings - Fork 3
/
solution.ts
49 lines (38 loc) · 1.03 KB
/
solution.ts
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
/*
* @lc app=leetcode id=112 lang=javascript
*
* [112] Path Sum
*/
// @lc code=start
type TreeNode = {
val: number;
left: MaybeTreeNode;
right: MaybeTreeNode;
};
type MaybeTreeNode = TreeNode | null;
/**
* 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} sum
* @return {boolean}
*/
const hasPathSum = (root: MaybeTreeNode, sum: number): boolean => {
// * ['60 ms', '95.32 %', '37.3 MB', '49.22 %']
if (root === null) return false;
const dfsSearch = (node: TreeNode, remain: number): boolean => {
const { left: l, right: r, val: v } = node;
remain -= v;
if (l === null && r === null && remain === 0) return true;
return (l !== null && dfsSearch(l, remain)) || (r !== null && dfsSearch(r, remain));
};
return dfsSearch(root, sum);
};
// @lc code=end
export { hasPathSum };