def testBuildTree(self): s = Solution() inorder = [1,2,3,4] postorder = [2,4,3,1] an = [1, '#', 3, 2, 4] self.assertEqual(saveTree(s.buildTree(inorder, postorder)), an)
def testBuildTree(self): s = Solution() inorder = [1,2,3,4] preorder = [1,3,2,4] an = [1, '#', 3, 2, 4] self.assertEqual(saveTree(s.buildTree(preorder, inorder)), an)
def testSortedArrayToBST(self): s = Solution() nums = [[1, 2, 2, 3, 3, 4, 4], range(1, 9)] ans = [[3, 2, 4, 1, 2, 3, 4], [5, 3, 7, 2, 4, 6, 8, 1]] for num, an in zip(nums, ans): self.assertEqual(saveTree(s.sortedArrayToBST(num)), an, "result not agree")
def testGenerateTrees(self): s = Solution() an = [[1,'#',2,'#',3],[2,1,3],[3,1,'#','#',2],[1,'#',3,2],[3,2,'#',1]] A = [saveTree(root) for root in s.generateTrees(3)] self.assertEqual(A, an)
def testGrayCode(self): s = Solution() head = getList(range(5)) an = [2, 1, 4, 0, '#', 3] self.assertEqual(saveTree(s.sortedListToBST(head)), an)