using System.Collections.Generic;
namespace ValidBinarySearchTree
public Node(int value, Node? left, Node? right) { Value = value; Left = left; Right = right; }
public int Value { get; set; }
public Node? Left { get; set; }
public Node? Right { get; set; }
static void Main(string[] args)
var trees = CreateTreesForExercise();
for (int idx = 0; idx < trees.Count; idx++)
var treeNumber = idx + 1;
var treeIsValid = IsBinarySearchTreeValid(trees[idx], int.MinValue, int.MaxValue);
Console.WriteLine($"Tree {treeNumber} is valid: {treeIsValid}");
static bool IsBinarySearchTreeValid(Node? treeNode, int low, int high)
static List<Node> CreateTreesForExercise()
var trees = new List<Node>();
Node tree1 = new(15, new(10, new(5, null, null), new(12, null, null)), new(25, new(17, null, null), new(27, null, null)));
Node tree2 = new(3, new(2, new(1, null, null), new(4, null, null)), new(5, new(4, null, null), new(7, null, null)));