/// <summary> /// Recursive method to check whether the given binary tree is a valid binary search tree or not. /// </summary> /// <param name="node"></param> /// <param name="MIN"></param> /// <param name="MAX"></param> /// <returns>true/false</returns> private bool IsValidBST(Node node, int MIN = int.MinValue, int MAX = int.MaxValue) { try { if (node == null) return true; if (node.Value > MIN && node.Value < MAX && IsValidBST(node.Left, MIN, node.Value) && IsValidBST(node.Right, node.Value, MAX)) return true; else return false; } catch (Exception ex) { Logger.Log(string.Format("Exception at BinarySearchTree.IsValidBST {0} ST {1}", ex.Message, ex.StackTrace)); return false; } }
/// <summary> /// Constructor to initialize the node values /// </summary> public BinarySearchTree() { n1 = new Node(1, null, null); n3 = new Node(3, null, null); n2 = new Node(2, n1, n3); }
public Node(int value, Node left, Node right) { Value = value; Left = left; Right = right; }