```
class Solution {
public:
long long cur;
void trav(TreeNode* n, double t) {
if (n==NULL) return;
if ( abs(n->val - t) < abs(cur-t) ) cur = n->val;
if (t<n->val) trav(n->left,t);
else trav(n->right,t);
}
int closestValue(TreeNode* root, double target) {
cur = root->val;
trav(root,target);
return cur;
}
};
```