Minimum Absolute Difference in BST Java solution


  • 0
    M
    class Solution {
    public:
        int getMinimumDifference(TreeNode* root) {
            if (root == nullptr) {
                return INT_MAX;
            }
            
            TreeNode* l = root->left;
            TreeNode* r = root->right;
            
            int ld = INT_MAX;
            while (l != nullptr) {
                ld = root->val - l->val;
                l = l->right;
            }
            
            int rd = INT_MAX;
            while (r != nullptr) {
                rd = r->val - root->val;
                r = r->left;
            }
      
            return min(min(ld, rd), min(getMinimumDifference(root->left), getMinimumDifference(root->right)));
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.