My code passes at PC but fail when submitted to LeetCode.


  • 0
    W

    I do an Inorder Traversal in Morris Algorithm. Then I see whether elements in the vector are ascendant.

    class Solution {
        public:
        	bool isValidBST(TreeNode *root)
        	{
        		bool flag = true;
        		if (root == nullptr)
        			return true;
        		if (root->left == nullptr&&root->right == nullptr)
        			return true;
        		vector<int> inorder;
        		TreeNode *tmp = nullptr;
        		while (root)
        		{
        			if (root->left)
        			{
        				tmp = root->left;
        				while (tmp->right&&tmp->right != root)
        				{
        					tmp = tmp->right;
        				}
        				if (tmp->right == root)
        				{
        					inorder.push_back(root->val);
        					root = root->right;
        				}
        				else
        				{
        					tmp->right = root;
        					root = root->left;
        				}
        			}
        			else
        			{
        				inorder.push_back(root->val);
        				root = root->right;
        			}
        		}
        		for (vector<int>::iterator i = inorder.begin() + 1; i != inorder.end(); i++)
        		{
        			if (*i <= *(i - 1))
        				flag = false;
        		}
        		return flag;
        	}
        };

Log in to reply
 

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