Minimum Absolute Difference in BST LeetCode solution in C++
1class Solution {2public:3int inorder(TreeNode *root, int &prevElement, int &ans) {4if (root->left) inorder(root->left, prevElement, ans);5if (prevElement >= 0) ans = min(ans, abs(prevElement - root->val));6prevElement = root->val;7if (root->right) inorder(root->right, prevElement, ans);89return ans;10}11int getMinimumDifference(TreeNode *root) {12int prevElement = -1;13int ans = INT_MAX;14return inorder(root, prevElement, ans);15}16};