Skip to content

Commit d3962e9

Browse files
committed
2 parents d9ab7dc + d094877 commit d3962e9

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

530/my_solution.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class TreeNode {
2+
constructor(val, left, right) {
3+
this.val = (val === undefined ? 0 : val);
4+
this.left = (left === undefined ? null : left);
5+
this.right = (right === undefined ? null : right);
6+
}
7+
}
8+
/**
9+
* @param {TreeNode} root
10+
* @return {number}
11+
*/
12+
const getMinimumDifference = (root) => {
13+
console.log(Infinity - 1)
14+
console.log(1 - Infinity)
15+
let queue = [root], min = Infinity;
16+
17+
while (queue.length > 0) {
18+
console.log("queue 1");
19+
console.log(queue);
20+
21+
let curr = queue.shift();
22+
console.log("Processing");
23+
console.log(curr);
24+
let l = -Infinity, r = Infinity;
25+
if (null !== curr.left) {
26+
l = curr.left.val;
27+
queue.push(curr.left);
28+
}
29+
30+
if (null !== curr.right) {
31+
r = curr.right.val;
32+
queue.push(curr.right);
33+
}
34+
console.log("queue 2");
35+
console.log(queue);
36+
37+
console.log(`min:${min}, q-l:${curr.val - l}, r-q:${r - curr.val}`)
38+
39+
min = Math.min(min, curr.val - l, r - curr.val);
40+
}
41+
42+
return min;
43+
};
44+
45+
// [4,2,6,1,3]
46+
let x = getMinimumDifference(new TreeNode(4,
47+
new TreeNode(2, new TreeNode(1), new TreeNode(3)), new TreeNode(6, new TreeNode(5))))
48+
console.log("x")
49+
console.log(x)

530/solution.js

Whitespace-only changes.

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
- [417. Pacific Atlantic Water Flow](./417/)
6262
- [424. Longest Repeating Character Replacement](./424/)
6363
- [435. Non-overlapping Intervals](./435/)
64+
- [530. Minimum Absolute Difference in BST](./530/)
6465
- [647. Palindromic Substrings](./647/)
6566
- [771. Jewels and Stones](./771/)
6667
- [997. Find the Town Judge](./997/)
@@ -112,7 +113,7 @@ Batch create:
112113
NOTE: JS IS HERE
113114
-->
114115
```ssh
115-
chapter=1365 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
116+
chapter=530 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
116117
```
117118
> then you can use `x` for quick debug.
118119

0 commit comments

Comments
 (0)