Ejemplo n.º 1
0
            public void Test2()
            {
                // Arrange
                var root = new TreeNode
                {
                    val    = 10
                    , left = new TreeNode
                    {
                        val    = 5
                        , left = new TreeNode
                        {
                            val     = -2
                            , right = new TreeNode
                            {
                                val    = 2
                                , left = new TreeNode
                                {
                                    val = -1
                                }
                            }
                        }
                        , right = new TreeNode
                        {
                            val     = 6
                            , right = new TreeNode
                            {
                                val = 8
                            }
                        }
                    }
                    , right = new TreeNode
                    {
                        val     = 30
                        , right = new TreeNode
                        {
                            val = 40
                        }
                    }
                };

                // Act
                // Assert
                var it = new BSTIterator(root);

                Assert.AreEqual(-2, it.Next());
                Assert.AreEqual(-1, it.Next());
                Assert.True(it.HasNext());
                Assert.AreEqual(2, it.Next());
                Assert.AreEqual(5, it.Next());
                Assert.AreEqual(6, it.Next());
                Assert.AreEqual(8, it.Next());
                Assert.AreEqual(10, it.Next());
                Assert.AreEqual(30, it.Next());
                Assert.True(it.HasNext());
                Assert.AreEqual(40, it.Next());
                Assert.False(it.HasNext());
            }
Ejemplo n.º 2
0
        public void BinarySearchTreeIterator_Solutions()
        {
            var root = TreeNode.GetTree(8, 3, 10, 1, 6, null, 14, null, null, 4, 7, null, null, 13, null);

            var i = new BSTIterator(root);

            var list = new List <int>();

            while (i.HasNext())
            {
                list.Add(i.Next());
            }

            AssertEnumerable.AreEqual(new[] { 1, 3, 4, 6, 7, 8, 10, 13, 14 }, list.ToArray());
        }
Ejemplo n.º 3
0
            public void Test1()
            {
                // Arrange
                var root = new TreeNode
                {
                    val    = 7
                    , left = new TreeNode
                    {
                        val = 3
                    }
                    , right = new TreeNode
                    {
                        val    = 15
                        , left = new TreeNode
                        {
                            val = 9
                        }
                        , right = new TreeNode
                        {
                            val = 20
                        }
                    }
                };

                // Act
                // Assert
                var it = new BSTIterator(root);

                Assert.AreEqual(3, it.Next());
                Assert.AreEqual(7, it.Next());
                Assert.True(it.HasNext());
                Assert.AreEqual(9, it.Next());
                Assert.True(it.HasNext());
                Assert.AreEqual(15, it.Next());
                Assert.True(it.HasNext());
                Assert.AreEqual(20, it.Next());
                Assert.False(it.HasNext());
            }