Esempio n. 1
0
        public void Invert_a_binary_tree()
        {
            //Given
            var invertBinaryTree = new InvertBinaryTree();



            var tree3 = new TreeNode(1);
            var tree4 = new TreeNode(3);
            var tree5 = new TreeNode(6);
            var tree6 = new TreeNode(9);

            var tree1 = new TreeNode(2, tree3, tree4);
            var tree2 = new TreeNode(7, tree5, tree6);

            var rootTree = new TreeNode(4, tree1, tree2);

            //When
            var result = invertBinaryTree.InvertTree(rootTree);

            //Then
            result.left.val.Should().Be(7);
            result.right.val.Should().Be(2);

            result.left.left.val.Should().Be(9);
            result.left.right.val.Should().Be(6);
            result.right.left.val.Should().Be(3);
            result.right.right.val.Should().Be(1);
        }
Esempio n. 2
0
        public static void InvertBinaryTreeTest()
        {
            var root = TreeUtils.CreateRandomTree();

            TreeUtils.PrintTree(root);
            Console.WriteLine();
            root = InvertBinaryTree.InvertTree(root);
            //Assert.AreEqual(root.left.data, inverted.right.data);
            //Assert.AreEqual(root.right.data, inverted.left.data);

            TreeUtils.PrintTree(root);
        }
        public void InvertTreeTests()
        {
            InvertBinaryTree obj = new InvertBinaryTree();

            TreeNode node = new TreeNode(1)
            {
                left = new TreeNode(2)
                {
                },
                right = new TreeNode(3)
                {
                }
            };

            var x = obj.InvertTree(node);//

            node = new TreeNode(4)
            {
                left = new TreeNode(9)
                {
                    left = new TreeNode(5)
                    {
                    },
                    right = new TreeNode(1)
                    {
                    }
                },
                right = new TreeNode(0)
                {
                }
            };

            x = obj.InvertTree(node);//

            node = new TreeNode(3)
            {
                left = new TreeNode(9)
                {
                },
            };

            obj.InvertTree(node);//

            node = new TreeNode(0)
            {
                left = new TreeNode(0)
                {
                    left = new TreeNode(5)
                    {
                    },
                    right = new TreeNode(1)
                    {
                    }
                },
                right = new TreeNode(0)
                {
                }
            };

            x = obj.InvertTree(node);//
        }