伊朗和叙利亚总统# Joke - 肚皮舞运动
o*d
1 楼
今天发生两次了,c++, 同样的test case,我用VS2010和cygwin都测试没问题,leetcode就
是fail........
比如这个:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isValidBST(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
// Start typing your C/C++ solution below
// DO NOT write int main() function
static TreeNode* prevNode = NULL;
if (!root)
return true;
//in-order trav
if(!isValidBST(root->left))
return false;
if((prevNode)&&(prevNode->val>=root->val))
return false;
prevNode = root;
if(!isValidBST(root->right))
return false;
return true;
}
};
//--------------测试结果
input output expected
{} true true
{0} true true
{1} true true
{1,1} false false
{1,#,1} false false
{0,-1} false true
{0,#,-1} false false
{0,1} false false
{0,#,1} true true
{2,1,3} true true
{10,5,15,#,#,6,20} false false
{3,1,5,0,2,4,6} false true
{3,1,5,0,2,4,6,#,#,#,3} false false
但是我用VS2010测过{0,-1}的case,没问题啊
//---------my test code-----------
int main()
{
TreeNode node(0);
TreeNode left(-1);
node.left = &left;
Solution sol;
bool bT = sol.isValidBST(&node);
cout << bT << endl;
}
是fail........
比如这个:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isValidBST(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
// Start typing your C/C++ solution below
// DO NOT write int main() function
static TreeNode* prevNode = NULL;
if (!root)
return true;
//in-order trav
if(!isValidBST(root->left))
return false;
if((prevNode)&&(prevNode->val>=root->val))
return false;
prevNode = root;
if(!isValidBST(root->right))
return false;
return true;
}
};
//--------------测试结果
input output expected
{} true true
{0} true true
{1} true true
{1,1} false false
{1,#,1} false false
{0,-1} false true
{0,#,-1} false false
{0,1} false false
{0,#,1} true true
{2,1,3} true true
{10,5,15,#,#,6,20} false false
{3,1,5,0,2,4,6} false true
{3,1,5,0,2,4,6,#,#,#,3} false false
但是我用VS2010测过{0,-1}的case,没问题啊
//---------my test code-----------
int main()
{
TreeNode node(0);
TreeNode left(-1);
node.left = &left;
Solution sol;
bool bT = sol.isValidBST(&node);
cout << bT << endl;
}