Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternate C++ solution #98. Validate Binary Search Tree #368

Closed
resyfer opened this issue Jul 6, 2022 · 4 comments
Closed

Alternate C++ solution #98. Validate Binary Search Tree #368

resyfer opened this issue Jul 6, 2022 · 4 comments

Comments

@resyfer
Copy link

resyfer commented Jul 6, 2022

A C++ solution to the 98.Validate Binary Search Tree problem which follows Neetcode's explanation in the video.

@farhan523
Copy link
Contributor

farhan523 commented Jul 9, 2022

#369

 class Solution
 {
    vector<int> res;
    bool inorderTraversal(TreeNode *root)
    {
        if (root == NULL)
            return true;
        bool r1, r2;
        if (root->left != NULL && root->left->val >= root->val)
            return false;
        else
            r1 = inorderTraversal(root->left);
        res.push_back(root->val);
        if (root->right != NULL && root->right->val <= root->val)
            return false;
        else
            r2 = inorderTraversal(root->right);
        return r1 && r2;
    }

    public:
        bool isValidBST(TreeNode *root)
        {
            if (!inorderTraversal(root))
                return false;
            for (int i = 1; i < res.size(); i++)
            {

                if (res[i] <= res[i - 1])
                    return false;
            }
            return true;
        }
};

@resyfer
Copy link
Author

resyfer commented Jul 9, 2022

Thank you for the solution, but my issue was created for a possible addition of my solution to the question which followed the solution in the video, and I've added that in the commit following the issue

@Ahmad-A0
Copy link
Collaborator

Ahmad-A0 commented Jul 9, 2022

I'll mention #424 which is also about this problem.

@Ahmad-A0
Copy link
Collaborator

Ahmad-A0 commented Aug 8, 2022

I'll close this issue as a parallel PR exists.

@Ahmad-A0 Ahmad-A0 closed this as completed Aug 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants