예제 #1
0
        public void TestMethod1(string[] nodes, string expected)
        {
            // Arrange
            SerializeandDeserializeBinaryTree question = new SerializeandDeserializeBinaryTree();
            TreeNode root = null;

            if (nodes != null && nodes.Length > 0)
            {
                root = new TreeNode(int.Parse(nodes[0]));
                List <TreeNode> nodesPreviousLevel = new List <TreeNode>()
                {
                    root
                };

                int  nodesInLevel = 2;
                bool left         = true;

                int index = 1;
                while (index < nodes.Length)
                {
                    List <TreeNode> nodesCurrentLevel = new List <TreeNode>();

                    int prevIndex = 0;
                    for (int i = 0; i < nodesInLevel && index < nodes.Length; i++)
                    {
                        if (nodes[index] != null)
                        {
                            TreeNode node = new TreeNode(int.Parse(nodes[index]));

                            nodesCurrentLevel.Add(node);
                            if (left)
                            {
                                nodesPreviousLevel[prevIndex].left = node;
                            }
                            else
                            {
                                nodesPreviousLevel[prevIndex].right = node;
                            }
                        }

                        left = !left;
                        if (left)
                        {
                            prevIndex++;
                        }

                        index++;
                    }

                    nodesInLevel       = nodesCurrentLevel.Count * 2;
                    nodesPreviousLevel = nodesCurrentLevel;
                }
            }

            // Act
            string actual = question.serialize(root);

            // Assert
            Assert.AreEqual(expected, actual);
        }
예제 #2
0
        public void TestMethod4()
        {
            // Arrange
            SerializeandDeserializeBinaryTree question = new SerializeandDeserializeBinaryTree();
            string serialized = "[1,2,null,3,null,4,null,5]";

            // Act
            TreeNode actual = question.deserialize(serialized);
        }
예제 #3
0
        public void TestMethod3()
        {
            // Arrange
            SerializeandDeserializeBinaryTree question = new SerializeandDeserializeBinaryTree();
            TreeNode root = new TreeNode(1);

            root.left                = new TreeNode(2);
            root.left.left           = new TreeNode(3);
            root.left.left.left      = new TreeNode(4);
            root.left.left.left.left = new TreeNode(5);
            string expected = "[1,2,null,3,null,4,null,5]";

            // Act
            string actual = question.serialize(root);

            Assert.AreEqual(expected, actual);
        }
예제 #4
0
 public void Setup()
 {
     solution = new SerializeandDeserializeBinaryTree();
 }