8ms C solution


  • 2
    W
    bool isValidBST(struct TreeNode* root) {
    if (NULL == root)
    	return true;
    
    if (NULL != root->left)
    {
    	struct TreeNode* current = root->left;
    	while (current->right != NULL)
    	{
    		current = current->right;
    	}
    	if (current->val >= root->val)
    	{
    		return false;
    	}
    
    }
    
    if (NULL != root->right)
    {
    	struct TreeNode* current = root->right;
    	while (NULL != current->left)
    	{
    		current = current->left;
    	}
    	if ( current->val <= root->val)
    	{
    		return false;
    	}
    }
    
    if ( !isValidBST(root->left))
    {
    	return false;
    }
    
    if (!isValidBST(root->right))
    {
    	return false;
    }
    
    return true;
    

    }


Log in to reply
 

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