コード例 #1
0
 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);
         }
     }
 }
コード例 #2
0
        static HackerRankBinTree FindMax(HackerRankBinTree root)
        {
            HackerRankBinTree result = root;

            while (result.right != null)
            {
                result = result.right;
            }
            return(result);
        }
コード例 #3
0
        static HackerRankBinTree FindMin(HackerRankBinTree root)
        {
            HackerRankBinTree result = root;

            while (result.left != null)
            {
                result = result.left;
            }
            return(result);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
 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));
 }
コード例 #6
0
 static bool checkBST(HackerRankBinTree root)
 {
     return(isBSTUtil(root, int.MinValue, int.MaxValue));
 }