File tree 2 files changed +37
-0
lines changed
2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 457
457
748|[ Shortest Completing Word] ( ./0748-shortest-completing-word.js ) |Easy|
458
458
762|[ Prime Number of Set Bits in Binary Representation] ( ./0762-prime-number-of-set-bits-in-binary-representation.js ) |Easy|
459
459
763|[ Partition Labels] ( ./0763-partition-labels.js ) |Medium|
460
+ 783|[ Minimum Distance Between BST Nodes] ( ./0783-minimum-distance-between-bst-nodes.js ) |Easy|
460
461
784|[ Letter Case Permutation] ( ./0784-letter-case-permutation.js ) |Medium|
461
462
790|[ Domino and Tromino Tiling] ( ./0790-domino-and-tromino-tiling.js ) |Medium|
462
463
791|[ Custom Sort String] ( ./0791-custom-sort-string.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments