Skip to content

Commit a693e1d

Browse files
authored
feat: add js solution to lc problem: No.783 (doocs#2620)
1 parent 83e4cda commit a693e1d

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

solution/0700-0799/0783.Minimum Distance Between BST Nodes/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,24 @@ func abs(x int) int {
184184
}
185185
```
186186

187+
```js
188+
var minDiffInBST = function (root) {
189+
let ans = Number.MAX_SAFE_INTEGER,
190+
prev = Number.MAX_SAFE_INTEGER;
191+
const dfs = root => {
192+
if (!root) {
193+
return;
194+
}
195+
dfs(root.left);
196+
ans = Math.min(ans, Math.abs(root.val - prev));
197+
prev = root.val;
198+
dfs(root.right);
199+
};
200+
dfs(root);
201+
return ans;
202+
};
203+
```
204+
187205
<!-- tabs:end -->
188206

189207
<!-- end -->

solution/0700-0799/0783.Minimum Distance Between BST Nodes/README_EN.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,24 @@ func abs(x int) int {
171171
}
172172
```
173173

174+
```js
175+
var minDiffInBST = function (root) {
176+
let ans = Number.MAX_SAFE_INTEGER,
177+
prev = Number.MAX_SAFE_INTEGER;
178+
const dfs = root => {
179+
if (!root) {
180+
return;
181+
}
182+
dfs(root.left);
183+
ans = Math.min(ans, Math.abs(root.val - prev));
184+
prev = root.val;
185+
dfs(root.right);
186+
};
187+
dfs(root);
188+
return ans;
189+
};
190+
```
191+
174192
<!-- tabs:end -->
175193

176194
<!-- end -->
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var minDiffInBST = function (root) {
2+
let ans = Number.MAX_SAFE_INTEGER,
3+
prev = Number.MAX_SAFE_INTEGER;
4+
const dfs = root => {
5+
if (!root) {
6+
return;
7+
}
8+
dfs(root.left);
9+
ans = Math.min(ans, Math.abs(root.val - prev));
10+
prev = root.val;
11+
dfs(root.right);
12+
};
13+
dfs(root);
14+
return ans;
15+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy