I was looking to this exercise: Is Valid Binary Search Tree
I tried to do the exercise by myself without looking to the solution and my code is "much simpler" than the code I found at the previous link. Is there anyone that can review this code or provide me some test cases for this problem? Generating test cases is way more time consuming than coding.
/*
Question: TestDome - BinarySearchTree
Solution by Antonio Di Bacco 2020
Write a function that checks if a given binary tree is a valid
binary search tree. A binary search tree (BST) is a binary tree
where the value of each node is larger or equal to the values in
all the nodes in that node's left subtree and is smaller than the
values in all the nodes in that node's right subtree.
For example, for the following tree:
n1 (Value: 1, Left: null, Right: null)
n2 (Value: 2, Left: n1, Right: n3)
n3 (Value: 3, Left: null, Right: null)
Call to isValidBST(n2) should return true since a tree with root at
n2 is a valid binary search tree.
Explanation: Subtrees rooted at nodes n1 and n3 are valid binary
search trees as they have no children. A tree rooted at node n2 is
a valid binary search tree since its value (2) is larger or equal
to the largest value in its left subtree (1, rooted at n1) and is
smaller than the smallest value in its right subtree (3 rooted at
n3).
*/
#include <stdexcept>
#include <string>
#include <iostream>
#include <algorithm>
class Node
{
public:
Node(int value, Node* left, Node* right)
{
this->value = value;
this->left = left;
this->right = right;
}
int getValue() const
{
return value;
}
Node* getLeft() const
{
return left;
}
Node* getRight() const
{
return right;
}
private:
int value;
Node* left;
Node* right;
};
class BinarySearchTree
{
public:
static bool isValidBST(const Node& root)
{
if (root.getRight() == nullptr) {
if (root.getLeft() == nullptr) {
return true;
}
else {
if (root.getLeft()->getValue() < root.getValue())
return isValidBST(*(root.getLeft()));
else
return false;
}
}
else {
if (root.getLeft() == nullptr) {
if (root.getRight()->getValue() > root.getValue())
return isValidBST(*(root.getRight()));
else
return false;
}
else {
if ((root.getRight()->getValue() > root.getValue()) && (root.getLeft()->getValue() < root.getValue()))
return isValidBST(*(root.getLeft())) && isValidBST(*(root.getRight()));
else
return false;
}
}
}
private:
};
#ifndef RunTests
int main(int argc, const char* argv[])
{
Node n1(1, NULL, NULL);
Node n3(3, NULL, NULL);
Node N(2, &n1, &n3);
std::cout << BinarySearchTree::isValidBST(N) << std::endl;
Node m1(1, NULL, NULL);
Node m3(3, NULL, NULL);
Node M(2, &m3, &m1);
std::cout << BinarySearchTree::isValidBST(M) << std::endl;
Node p4(5, NULL, NULL);
Node p1(1, NULL, NULL);
Node p3(3, &p4, NULL);
Node P(2, &p1, &p3);
std::cout << BinarySearchTree::isValidBST(P) << std::endl;
return 0;
}
#endif
5in your third test with-5, you incorrectly report the tree to be a valid BST. \$\endgroup\$(3, (2, (1), (5)), (...)). It is not valid (5in the left subtree is greater than3at the root). \$\endgroup\$