C 9ms simple way ( no extra memory)


  • 0
    X

    Comparison of every node`s Minimum Absolute Difference

    int R_nodes(struct TreeNode *root)
    {
        if(root->left)
            return R_nodes(root->left);
        else
            return root->val;
    }
    
    int L_nodes(struct TreeNode *root)
    {
        if(root->right)
            return L_nodes(root->right);
        else
            return root->val;
    }
    
    
    int getMinimumDifference(struct TreeNode* root) {
        
        
        int R_min_diff = INT_MAX , L_min_diff =INT_MAX ;
        
        if(root->left)
        {
            L_min_diff = abs( root->val - L_nodes(root->left));
            int LF = getMinimumDifference(root->left) ;
            if(L_min_diff > LF )
                L_min_diff = LF;
        }
        if(root->right)
        {
            R_min_diff = abs( root->val - R_nodes(root->right));
            int RF = getMinimumDifference(root->right);
            if( R_min_diff > RF)
                R_min_diff = RF;
        }
        
        return R_min_diff<L_min_diff?R_min_diff:L_min_diff;
    }
    

Log in to reply
 

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