Why is my answer wrong for{87,84,94,79,#,#,#,77,#,-82,#,70,#,38,#,36,45,22,#,#,#,18,24,14...}


  • 0
    D
    class Solution {
    public:
    bool isValidBST(TreeNode *root, int lower, int upper)
    {
    	if (root == NULL)
    		return true;
    	if (!root->left&&!root->right)
    	return root->val > lower&&root->val < upper;
    	return isValidBST(root->left, lower, root->val)&&isValidBST(root->right, root->val, upper);
    }
    bool isValidBST(TreeNode *root)
    {
    	return isValidBST(root, INT_MIN, INT_MAX);
    }
    };

  • 1
    S
    bool isValidBST(TreeNode *root, int lower, int upper)
    {
        if (root == NULL)
            return true;
        if (!root->left&&!root->right)
        return root->val > lower&&root->val < upper; // lines below it are never touched
        
        return isValidBST(root->left, lower, root->val)&&isValidBST(root->right, root->val, upper);  
    }
    

    ------------------UPDATED----ABANDON ABOVE-------------------

    Look into this {5,14,#,1,#} , the tree looks like this:

          5
         /
       14
      /
    1

  • 0
    D

    what do you mean by "lines below it are never touched"?If root is a leaf,i think there is no nodes below root


  • 0
    S

    Sorry, I misunderstood this code, since there is no indentation in first return. There is room for improvement in code style though.


  • 0
    S

    I update my answer. I suggest to elaborate the idea based on code.


  • 0
    D

    i got it,thank you so much


Log in to reply
 

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