コード例 #1
0
        public void TestMethod2(string[] expected, string serialized)
        {
            // Arrange
            SerializeandDeserializeBinaryTree question = new SerializeandDeserializeBinaryTree();
            TreeNode root = null;

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

                int  nodesInLevel = 2;
                bool left         = true;

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

                    int prevIndex = 0;
                    for (int i = 0; i < nodesInLevel && index < expected.Length; i++)
                    {
                        if (expected[index] != null)
                        {
                            TreeNode node = new TreeNode(int.Parse(expected[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
            TreeNode actual = question.deserialize(serialized);
        }
コード例 #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 Test1()
        {
            var tree   = solution.deserialize("1,-,-");
            var result = solution.deserialize(solution.serialize(tree));

            Assert.AreEqual(true, CompareTrees(result, tree));
        }