-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
86 lines (64 loc) · 1.61 KB
/
script.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
/* Problem 15
This question was asked by Apple.
Given a binary tree, find a minimum path sum from root to a leaf.
For example, the minimum path in this tree is [10, 5, 1, -1], which has sum 15.
10
/ \
5 5
\ \
2 1
/
-1
*/
class TreeNode {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
}
/* Finding the minimum path from the root to the leaf
* @param {TreeNode} root
* @return {number[]}
*/
function minTreePath(root) {
const result = Number.MAX_VALUE;
return recurseTree(root, result);
}
/* Recursive calls to find the results
* @param {TreeNode} root
* @param {number} result
* @return {number}
*/
function recurseTree(root, result) {
if(root === null) return 0;
if(root.left == null && root.right == null) return root.val;
// Recurse left
const left = recurseTree(root.left, result);
// Recurse right
const right = recurseTree(root.right, result);
if(root.left !== null && root.right !== null) {
// Update the result if needed
result = Math.min(result, left + right + root.val);
// Returning the minimum possible value for root being on one side
return Math.min(left + root.val, right + root.val);
}
if(root.left === null){
return right + root.val;
} else if(root.right === null) {
return left + root.val;
}
return result;
}
const a = new TreeNode(10);
const b = new TreeNode(5);
const c = new TreeNode(2);
const d = new TreeNode(5);
const e = new TreeNode(1);
const f = new TreeNode(-1);
a.left = b;
a.right = d;
b.right = c;
d.right = e;
e.left = f;
console.log(minTreePath(a)); // 15