My simple c++ solution


  • 0
    K
    class Solution {
    public:
    int closestValue(TreeNode* root, double target) {
        int res=root->val;
        while(root)
        {
            res=abs(res-target) > abs(root->val-target)?root->val:res;
            root=target>root->val?root->right:root->left;
            
        }
        return res;
    }
    

    };


Log in to reply
 

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