4-line C++ code


  • 0
    G
        int closestValue(TreeNode* root, double target) {
            int a = root->val;
            if (target < root->val && root->left) a = closestValue(root->left, target);
            else if (target > root->val && root->right) a = closestValue(root->right, target);
            
            return (fabs(target - a) < fabs(target - root->val)) ? a : root->val;
        }
    

Log in to reply
 

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