Skip to content

Commit 8769ab5

Browse files
committed
Time: 61 ms (88.24%), Space: 42.3 MB (94.71%) - LeetHub
1 parent 08cad2c commit 8769ab5

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {number}
12+
*/
13+
var minDiffInBST = function(root) {
14+
let minDiff = Infinity;
15+
let prev = null;
16+
17+
function inorder(node) {
18+
if (node == null) {
19+
return;
20+
}
21+
22+
inorder(node.left);
23+
24+
if (prev != null) {
25+
minDiff = Math.min(minDiff, node.val - prev.val);
26+
}
27+
28+
prev = node;
29+
30+
inorder(node.right);
31+
}
32+
33+
inorder(root);
34+
35+
return minDiff;
36+
};

0 commit comments

Comments
 (0)