Пример #1
0
        public void SkewedTree()
        {
            BinaryTreeProblemsSumNodesEquidistantFromLeaves target = new BinaryTreeProblemsSumNodesEquidistantFromLeaves();

            Node root = new Node()
            {
                Value = 1
            };

            root.Left = new Node()
            {
                Value = 2
            };
            root.Left.Left = new Node()
            {
                Value = 3
            };
            root.Left.Left.Left = new Node()
            {
                Value = 4
            };
            root.Left.Left.Left.Left = new Node()
            {
                Value = 5
            };

            Assert.AreEqual(1, target.SumNodesBeforeLeafLevel(root, 4));
            Assert.AreEqual(2, target.SumNodesBeforeLeafLevel(root, 3));
            Assert.AreEqual(3, target.SumNodesBeforeLeafLevel(root, 2));
            Assert.AreEqual(4, target.SumNodesBeforeLeafLevel(root, 1));
            Assert.AreEqual(5, target.SumNodesBeforeLeafLevel(root, 0));

            Assert.AreEqual(0, target.SumNodesBeforeLeafLevel(root, -1));
            Assert.AreEqual(0, target.SumNodesBeforeLeafLevel(root, 5));
        }
Пример #2
0
        public void UnBalancedTree()
        {
            BinaryTreeProblemsSumNodesEquidistantFromLeaves target = new BinaryTreeProblemsSumNodesEquidistantFromLeaves();

            Node root = new Node()
            {
                Value = 1
            };

            root.Left = new Node()
            {
                Value = 2
            };
            root.Left.Left = new Node()
            {
                Value = 3
            };
            root.Left.Right = new Node()
            {
                Value = 4
            };
            root.Left.Right.Left = new Node()
            {
                Value = 5
            };
            root.Left.Right.Right = new Node()
            {
                Value = 6
            };

            Assert.AreEqual(1, target.SumNodesBeforeLeafLevel(root, 3));
            Assert.AreEqual(1 + 2, target.SumNodesBeforeLeafLevel(root, 2));
            Assert.AreEqual(2 + 4, target.SumNodesBeforeLeafLevel(root, 1));
            Assert.AreEqual(3 + 5 + 6, target.SumNodesBeforeLeafLevel(root, 0));

            Assert.AreEqual(0, target.SumNodesBeforeLeafLevel(root, 4));
        }