9ms C++ solution


  • 0
    W
    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;
        }
    };
    

Log in to reply
 

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