```
class Solution {
private:
int res = INT_MAX;
public:
int closestValue(TreeNode* root, double target) {
if(root==NULL) return res;
double cur = (double) (root->val);
if(cur==target) return root->val;
if(res==INT_MAX || abs(res-target)> abs(cur-target))
res = cur;
if(cur>target)
closestValue(root->left, target);
else
closestValue(root->right, target);
}
};
```