public void Insert(int value) { if (value <= data) { if (left == null) { left = new HackerRankBinTree(value); } else { left.Insert(value); } } else { if (right == null) { right = new HackerRankBinTree(value); } else { right.Insert(value); } } }
static HackerRankBinTree FindMax(HackerRankBinTree root) { HackerRankBinTree result = root; while (result.right != null) { result = result.right; } return(result); }
static HackerRankBinTree FindMin(HackerRankBinTree root) { HackerRankBinTree result = root; while (result.left != null) { result = result.left; } return(result); }
static void Main(string[] args) { HackerRankBinTree root = new HackerRankBinTree(3); //root.Insert(1);root.Insert(2);root.Insert(3); root.Insert(5); root.Insert(4); root.Insert(6); root.Insert(7); //bool a=checkDuplication(root.left, 2); root.left = new HackerRankBinTree(2); root.left.left = new HackerRankBinTree(1); root.left.right = new HackerRankBinTree(4); root.right = new HackerRankBinTree(6); root.right.left = new HackerRankBinTree(5); root.right.right = new HackerRankBinTree(7); bool b = checkBST(root); }
static bool isBSTUtil(HackerRankBinTree root, int min, int max) { if (root == null) { return(true); } if (root.data <= min || root.data >= max)//Eşitlik halinde bu koşula girmesi duplication olduğu anlamına gelir. { return(false); } return(isBSTUtil(root.left, min, root.data) && isBSTUtil(root.right, root.data, max)); }
static bool checkBST(HackerRankBinTree root) { return(isBSTUtil(root, int.MinValue, int.MaxValue)); }