-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_solution.js
118 lines (101 loc) · 3.11 KB
/
my_solution.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
class TreeNode {
constructor(val, left, right) {
this.val = (val === undefined ? 0 : val);
this.left = (left === undefined ? null : left);
this.right = (right === undefined ? null : right);
}
}
/**
* @param {TreeNode} root
* @return {number}
*/
const getMinimumDifference = (root) => {
let list = [], visitedList = [];
// while (queue.length > 0) {
// let curr = queue.shift(), left = curr.left, right = curr.right;
// list.push(curr.val);
// if (null !== left) queue.push(left);
// if (null !== right) queue.push(right);
// }
// TODO: Study actually how to do a preorder i think my solution is not the best here
const inOrder = (node) => {
console.log(`node.val:`);
console.log(node)
console.log("list");
console.log(list);
// console.log(visitedList);
if (null === node) {
// visitedList.pop(); // current
let parent = visitedList.pop();
if (undefined === parent) return;
// console.log("visitedList");
// console.log(visitedList);
// console.log("parent");
// console.log(parent);
list.push(parent.val);
parent.left = null;
return inOrder(parent.right);
} else {
visitedList.push(node);
return inOrder(node.left);
}
// else {
// // else if (null === node.right) {
// // list.push(node.val);
// // let parent = visitedList.pop();
// // parent.left == null;
// // return inOrder(parent);
// // } else if (null !== node.left) {
// // visitedList.push(node);
// // return inOrder(node.left);
// // } else if (null !== node.right) {
// // visitedList.push(node);
// // return inOrder(node.right);
// // }
// }
// return inOrder();
}
inOrder(root);
console.log(visitedList);
console.log("list");
console.log(list);
// let min = Infinity, tmpMax = 0, tmpMin = Infinity;
// for (let i = 0; i < list.length; i++) {
// tmpMax = Math.max(tmpMax, list[i]);
// tmpMin = Math.min(tmpMin, list[i]);
// if (undefined !== list[i + 1]) {
// min = Math.min(min, Math.abs(list[i + 1] - tmpMax), Math.abs(list[i + 1] - tmpMin))
// }
// }
// let min = Infinity;
// bruteforce
// for (let i = 0; i < list.length; i++) {
// for (let j = 0; j < list.length; j++) {
// if (i === j) continue;
// min = Math.min(min, Math.abs(list[i] - list[j]))
// }
// }
let prev = list.shift(), min = Infinity;
while (list.length > 0) {
let curr = list.shift();
min = Math.min(min, curr - prev);
prev = curr;
}
return min;
};
// [4,2,6,1,3]
// let x = getMinimumDifference(new TreeNode(4,
// new TreeNode(2, new TreeNode(1), new TreeNode(3)), new TreeNode(6, new TreeNode(5))))
// [236,104,701,null,227,null,911] // 9
let x = getMinimumDifference(
new TreeNode(236,
new TreeNode(104, null, new TreeNode(227)),
new TreeNode(701, null, new TreeNode(911))))
// [1,0,48,null,null,12,49]
// let x = getMinimumDifference(
// new TreeNode(1,
// new TreeNode(0),
// new TreeNode(48,
// new TreeNode(12), new TreeNode(49))))
console.log("x")
console.log(x)