Clean small C++ code. 3 line implementation 16ms


  • 6
    N
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        bool val(TreeNode* root,long long int minrange,long long int maxrange)
        {
            if(root==NULL)return true;
            if(root->val>minrange&&root->val<maxrange&&val(root->left,minrange,root->val)&&val(root->right,root->val,maxrange))return true;
            return false;
        }
        bool isValidBST(TreeNode* root) {
            return val(root,-2147483650,2147483650);
        }
    };

Log in to reply
 

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