Minimum Absolute Difference in BST LeetCode solution in C++

1
class Solution {
2
public:
3
int inorder(TreeNode *root, int &prevElement, int &ans) {
4
if (root->left) inorder(root->left, prevElement, ans);
5
if (prevElement >= 0) ans = min(ans, abs(prevElement - root->val));
6
prevElement = root->val;
7
if (root->right) inorder(root->right, prevElement, ans);
8

9
return ans;
10
}
11
int getMinimumDifference(TreeNode *root) {
12
int prevElement = -1;
13
int ans = INT_MAX;
14
return inorder(root, prevElement, ans);
15
}
16
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0