コード例 #1
0
        public void TestConvertSortedListToBST()
        {
            var root = ConstructTree.ConvertSortedListToBST(MyLinkList.BuildListNodeFromArray(new [] { -10, -3, 0, 5, 9 }));
            //因为可以有多个结果可能,所以只是调用了isvalid方法验证是否是bst 和root不为null
            var r = ValidateBinarySearchTree.IsValid(root);

            Assert.AreEqual(r, true);
            Assert.IsNotNull(root);
        }
コード例 #2
0
        public void IsValidBSTTest3()
        {
            TreeNode root     = null;
            bool     expected = true;

            bool result = new ValidateBinarySearchTree().IsValidBST(root);

            Assert.AreEqual(expected, result);
        }
        public void TestMethod2()
        {
            var node = new TreeNode {
                val = 2147483647
            };
            var result = new ValidateBinarySearchTree().IsValidBST(node);

            Assert.IsTrue(result);
        }
コード例 #4
0
        public void IsValidBSTTest4()
        {
            TreeNode root = new TreeNode(1);

            root.left = new TreeNode(1);
            bool expected = false;

            bool result = new ValidateBinarySearchTree().IsValidBST(root);

            Assert.AreEqual(expected, result);
        }
コード例 #5
0
        public void ValidateBSTTests(object[] treeNodes, bool expectedIsValidBST)
        {
            // arrange
            TreeNode root = TreeNode.Build(treeNodes);

            // act
            bool isValid = ValidateBinarySearchTree.IsValid(root);

            // assert
            Assert.AreEqual(expectedIsValidBST, isValid, $"Failed validating for tree: { string.Join("-", treeNodes) }");
        }
コード例 #6
0
        public void IsValidBSTTest()
        {
            int?[] treeNodes1 = new int?[] { 4, 1, 5, null, null, 3, 6 }; //false  //{ 2, 1, 3 }; //truee

            int?[] treeNodes2 = new int?[] { 5, 1, 4, null, null, 3, 6 }; // false

            TreeNode root1 = Helper.BuildTreeFromLevelOrderArray(treeNodes1, 0);

            TreeNode root2 = Helper.BuildTreeFromLevelOrderArray(treeNodes2, 0);

            System.Console.WriteLine(ValidateBinarySearchTree.IsValidBSTIteration(root1));
            //System.Console.WriteLine(ValidateBinarySearchTree.IsValidBST(root2));
        }
        public void TestMethod1()
        {
            var node = new TreeNode {
                val = 2, left = new TreeNode {
                    val = 1
                }, right = new TreeNode {
                    val = 3
                }
            };
            var result = new ValidateBinarySearchTree().IsValidBST(node);

            Assert.IsTrue(result);
        }
コード例 #8
0
        public void TestValidateBinarySearchTree()
        {
            var tree = Build1();
            var r    = ValidateBinarySearchTree.IsValid(tree);

            Assert.AreEqual(r, false);

            tree = Build2();
            r    = ValidateBinarySearchTree.IsValid(tree);
            Assert.AreEqual(r, true);

            tree = Build3();
            r    = ValidateBinarySearchTree.IsValid(tree);
            Assert.AreEqual(r, false);
        }
コード例 #9
0
        public void IsValidBSTTest5()
        {
            //[10,5,15,null,null,6,20]
            TreeNode root = new TreeNode(10);

            root.left        = new TreeNode(5);
            root.right       = new TreeNode(15);
            root.right.left  = new TreeNode(6);
            root.right.right = new TreeNode(20);
            bool expected = false;

            bool result = new ValidateBinarySearchTree().IsValidBST(root);

            Assert.AreEqual(expected, result);
        }
コード例 #10
0
        public void ValidateBSTResultsInSucess()
        {
            var treeNode  = new TreeNode(2);
            var treeNode1 = new TreeNode(1);
            var treeNode3 = new TreeNode(3);

            treeNode.Left  = treeNode1;
            treeNode.Right = treeNode3;



            var result = new ValidateBinarySearchTree().IsValidBST(treeNode);

            Assert.IsTrue(result);
        }
コード例 #11
0
        public void ValidateBSTResultsInFailureForItertiveImpl()
        {
            var treeNode  = new TreeNode(5);
            var treeNode1 = new TreeNode(1);
            var treeNode4 = new TreeNode(4);
            var treeNode3 = new TreeNode(3);
            var treeNode6 = new TreeNode(6);

            treeNode.Left  = treeNode1;
            treeNode.Right = treeNode4; //this violates the BST property

            treeNode4.Left  = treeNode3;
            treeNode4.Right = treeNode6;



            var result = new ValidateBinarySearchTree().IsValidBSTIterative(treeNode);

            Assert.IsFalse(result);
        }
コード例 #12
0
        public void IsValidBSTTest()
        {
            ValidateBinarySearchTree fd = new ValidateBinarySearchTree();

            TreeNode t = new TreeNode(1);

            t.left = new TreeNode(1);

            Assert.IsFalse(fd.IsValidBST(t));//false

            t = new TreeNode(10);

            t.left = new TreeNode(5);

            t.right = new TreeNode(15)
            {
                left  = new TreeNode(6),
                right = new TreeNode(20)
            };

            Assert.IsFalse(fd.IsValidBST(t));//false

            t = new TreeNode(3);

            t.left = new TreeNode(1)
            {
                left  = new TreeNode(0),
                right = new TreeNode(2)
            };

            t.right = new TreeNode(5)
            {
                left  = new TreeNode(4),
                right = new TreeNode(6)
            };

            Assert.IsTrue(fd.IsValidBST(t));//true
        }