Can someone tell me what's wrong with my code? (Check if a Binary Tree is a BST)


  • -1
    M

    /**

    • Definition for a binary tree node.
    • struct TreeNode {
    • int val;
      
    • struct TreeNode *left;
      
    • struct TreeNode *right;
      
    • };
      */

    bool checkLeftSubTreeValues(struct TreeNode* root, int data)
    {
    if(root == NULL)
    {
    return 1;
    }

    if(data < (root->val))
    {
        return 0;
    }
    
    if(checkLeftSubTreeValues(root->left, data) == 0)
    {
        return 0;
    }
    
    if(checkLeftSubTreeValues(root->right, data) == 0)
    {
        return 0;
    }
    
    return 1;
    

    }

    bool checkRightSubTreeValues(struct TreeNode* root, int data)
    {
    if(root == NULL)
    {
    return 1;
    }

    if(data > (root->val))
    {
        return 0;
    }
    
    if(checkRightSubTreeValues(root->left, data) == 0)
    {
        return 0;
    }
    
    if(checkRightSubTreeValues(root->right, data) == 0)
    {
        return 0;
    }
    
    return 1;
    

    }

    bool isValidBST(struct TreeNode* root)
    {
    if(root == NULL)
    {
    return 1;
    }

    if(checkLeftSubTreeValues(root->left, root->val)==0)
    {
        return 0;
    }
    
    if(checkRightSubTreeValues(root->right, root->val)==0)
    {
        return 0;
    }
    
    if(isValidBST(root->left)==0)
    {
        return 0;
    }
    
    if(isValidBST(root->right)==0)
    {
        return 0;
    }
    
    return 1;
    

    }


Log in to reply
 

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