forked from adamespii/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem_135.js
66 lines (58 loc) Β· 1.45 KB
/
problem_135.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
/* eslint no-else-return: ["error", {allowElseIf: true}] */
// TreeNode class
class TreeNode {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
}
/**
* Find minimum path from root to leaf
* @param {TreeNode} root
* @return {number[]}
*/
function minTreePath(root) {
const result = Number.MAX_VALUE;
return recurseTree(root, result);
}
/**
* Recursive calls to find result
* @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 result if needed
result = Math.min(result, left + right + root.val);
// Return 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