Skip to content

Commit b4cff90

Browse files
committedMar 2, 2025
Add solution #783
1 parent 78d7705 commit b4cff90

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@
457457
748|[Shortest Completing Word](./0748-shortest-completing-word.js)|Easy|
458458
762|[Prime Number of Set Bits in Binary Representation](./0762-prime-number-of-set-bits-in-binary-representation.js)|Easy|
459459
763|[Partition Labels](./0763-partition-labels.js)|Medium|
460+
783|[Minimum Distance Between BST Nodes](./0783-minimum-distance-between-bst-nodes.js)|Easy|
460461
784|[Letter Case Permutation](./0784-letter-case-permutation.js)|Medium|
461462
790|[Domino and Tromino Tiling](./0790-domino-and-tromino-tiling.js)|Medium|
462463
791|[Custom Sort String](./0791-custom-sort-string.js)|Medium|
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* 783. Minimum Distance Between BST Nodes
3+
* https://leetcode.com/problems/minimum-distance-between-bst-nodes/
4+
* Difficulty: Easy
5+
*
6+
* Given the root of a Binary Search Tree (BST), return the minimum difference between
7+
* the values of any two different nodes in the tree.
8+
*/
9+
10+
/**
11+
* Definition for a binary tree node.
12+
* function TreeNode(val, left, right) {
13+
* this.val = (val===undefined ? 0 : val)
14+
* this.left = (left===undefined ? null : left)
15+
* this.right = (right===undefined ? null : right)
16+
* }
17+
*/
18+
/**
19+
* @param {TreeNode} root
20+
* @return {number}
21+
*/
22+
var minDiffInBST = function(root) {
23+
let result = Infinity;
24+
let previous = null;
25+
26+
inorder(root);
27+
return result;
28+
29+
function inorder(node) {
30+
if (!node) return;
31+
inorder(node.left);
32+
result = previous === null ? result : Math.min(result, node.val - previous);
33+
previous = node.val;
34+
inorder(node.right);
35+
}
36+
};

0 commit comments

Comments
 (0)
Please sign in to comment.