Example #1
0
        public void FindsPaths()
        {
            var inputOne = TreeNode.BuildTreeOne();
            var pathsOne = PathSum.GetPaths(inputOne, 9);

            Assert.Equal(new List <List <int> > {
                new List <int> {
                    5, 3, 1
                }
            }, pathsOne);

            var inputTwo = TreeNode.BuildTreeThree();
            var pathsTwo = PathSum.GetPaths(inputTwo, 17);

            Assert.Equal(new List <List <int> > {
                new List <int> {
                    3, 5, 2, 7
                }
            }, pathsTwo);

            var inputThree    = TreeNode.BuildTreeFour();
            var pathsThree    = PathSum.GetPaths(inputThree, 14);
            var multiplePaths = new List <List <int> > {
                new List <int> {
                    3, 5, 6
                }, new List <int> {
                    3, 5, 2, 4
                }, new List <int> {
                    3, 1, 8, 2
                }
            };

            Assert.Equal(multiplePaths, pathsThree);
        }
Example #2
0
        public void PathSumTestCase3()
        {
            var tree = new MyBinaryTree(1);

            tree.Left = new MyBinaryTree(2);

            PathSum.HasPathSum(tree, 1).Should().Be(false);
        }
Example #3
0
        public void HasPathSumOfIterationTest1()
        {
            TreeNode root = null;

            bool expected = false;
            bool hasPath  = new PathSum().HasPathSumOfIteration(root, 22);

            Assert.AreEqual(expected, hasPath);
        }
Example #4
0
        public void FindsALowerCommonAncestor()
        {
            var inputOne  = TreeNode.BuildTreeOne();
            var hasSumOne = PathSum.HasPath(inputOne, 9);

            Assert.True(hasSumOne);

            var inputTwo  = TreeNode.BuildTreeThree();
            var hasSumTwo = PathSum.HasPath(inputTwo, 4);

            Assert.True(hasSumTwo);
        }
Example #5
0
        public void DoesNotFindNotExistingPaths()
        {
            var inputOne = TreeNode.BuildTreeTwo();
            var pathsOne = PathSum.GetPaths(inputOne, 9);

            Assert.Empty(pathsOne);

            var inputTwo = TreeNode.BuildTreeThree();
            var pathsTwo = PathSum.GetPaths(inputTwo, 9);

            Assert.Empty(pathsTwo);
        }
Example #6
0
        public void ReturnsLowestCommonAncestorForInheritedNodes()
        {
            var inputOne  = TreeNode.BuildTreeOne();
            var hasSumOne = PathSum.HasPath(inputOne, 8);

            Assert.False(hasSumOne);

            var inputTwo  = TreeNode.BuildTreeThree();
            var hasSumTwo = PathSum.HasPath(inputTwo, 18);

            Assert.False(hasSumTwo);
        }
Example #7
0
        public void PathSumTestCase1()
        {
            var tree = new MyBinaryTree(5);

            tree.Left            = new MyBinaryTree(4);
            tree.Left.Left       = new MyBinaryTree(11);
            tree.Left.Left.Left  = new MyBinaryTree(7);
            tree.Left.Left.Right = new MyBinaryTree(2);
            tree.Right           = new MyBinaryTree(8);
            tree.Right.Left      = new MyBinaryTree(4);
            tree.Right.Left.Left = new MyBinaryTree(1);
            tree.Right.Right     = new MyBinaryTree(13);

            PathSum.HasPathSum(tree, 22).Should().Be(true);
        }
Example #8
0
        public void TestMethod2()
        {
            // Arrange
            PathSum  question = new PathSum();
            TreeNode root     = new TreeNode(-2);

            root.right = new TreeNode(-3);
            bool expected = true;

            // Act
            bool actual = question.HasPathSum(root, -5);

            // Assert
            Assert.AreEqual(expected, actual);
        }
Example #9
0
        public void HasPathSumTest()
        {
            TreeNode root = new TreeNode(5);

            root.left            = new TreeNode(4);
            root.left.left       = new TreeNode(11);
            root.left.left.left  = new TreeNode(7);
            root.left.left.right = new TreeNode(2);

            root.right             = new TreeNode(8);
            root.right.left        = new TreeNode(13);
            root.right.right       = new TreeNode(4);
            root.right.right.right = new TreeNode(1);

            bool expected = true;
            bool hasPath  = new PathSum().HasPathSum(root, 22);

            Assert.AreEqual(expected, hasPath);
        }
Example #10
0
        public void TestGetPathSum()
        {
            var root = new TreeNode(5);

            root.Left  = new TreeNode(4);
            root.Right = new TreeNode(8);

            root.Left.Left       = new TreeNode(11);
            root.Left.Left.Left  = new TreeNode(7);
            root.Left.Left.Right = new TreeNode(2);

            root.Right.Left        = new TreeNode(13);
            root.Right.Right       = new TreeNode(4);
            root.Right.Right.Right = new TreeNode(1);
            root.Right.Right.Left  = new TreeNode(5);

            var r = PathSum.GetPathSum(root, 22);

            Assert.AreEqual(r.Count, 2);
            Assert.AreEqual(r.All(i => i.Sum() == 22), true);
        }
Example #11
0
        public void TestPath1()
        {
            var root = new TreeNode(5);

            root.Left  = new TreeNode(4);
            root.Right = new TreeNode(8);

            root.Left.Left       = new TreeNode(11);
            root.Left.Left.Left  = new TreeNode(7);
            root.Left.Left.Right = new TreeNode(2);

            root.Right.Left        = new TreeNode(13);
            root.Right.Right       = new TreeNode(4);
            root.Right.Right.Right = new TreeNode(1);

            var r = PathSum.HasPathSum(root, 22);

            Assert.AreEqual(r, true);

            r = PathSum.HasPathSum(root, 13);
            Assert.AreEqual(r, false);
        }
Example #12
0
        public void TestMethod1()
        {
            // Arrange
            PathSum  question = new PathSum();
            TreeNode root     = new TreeNode(5);

            root.left              = new TreeNode(4);
            root.left.left         = new TreeNode(11);
            root.left.left.left    = new TreeNode(7);
            root.left.left.right   = new TreeNode(2);
            root.right             = new TreeNode(8);
            root.right.left        = new TreeNode(13);
            root.right.right       = new TreeNode(4);
            root.right.right.right = new TreeNode(1);
            bool expected = true;

            // Act
            bool actual = question.HasPathSum(root, 22);

            // Assert
            Assert.AreEqual(expected, actual);
        }
Example #13
0
        private static void TestPathSum()
        {
            PathSum instance = new PathSum();

            Console.WriteLine(instance.Solution(new TreeNode(1, new TreeNode(2, 3, 5), 4), 6));
        }