c++ recursive


  • 0
    B
    class Solution {
    public:
        int closestValue(TreeNode* root, double target) {
            if(root -> val > target)
            {
                if(!root -> left) return root->val;
                int val = closestValue(root -> left, target);
                return (val+root->val)/2. > target ? val : root -> val;
            }
            if(root -> val < target)
            {
                if(!root -> right) return root->val;
                int val = closestValue(root -> right, target);
                return (val+root->val)/2. < target ? val : root -> val;
            }
            return root -> val;
        }
    };

Log in to reply
 

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