def test_isSameTree(self): test_patterns = [ ([1, 2, 3], [1, 2, 3], True), ([1, 2], [1, None, 2], False), ([1, 2, 1], [1, 1, 2], False), ] for i, (arg1, arg2, expected) in enumerate(test_patterns): with self.subTest(test=i): s = main.Solution() tree1: TreeNode = createTreeNode(arg1) tree2: TreeNode = createTreeNode(arg2) self.assertEqual(s.isSameTree(tree1, tree2), expected)
def test_sumNumbers(self): test_patterns = [ ([1, 2, 3], 25), ] for i, (arg, expected) in enumerate(test_patterns): with self.subTest(test=i): s = main.Solution() tree: TreeNode = createTreeNode(arg) self.assertEqual(s.sumNumbers(tree), expected)
def test_pseudoPalindromicPaths(self): test_patterns = [ ([2, 3, 1, 3, 1, 0, 1], 2), ] for i, (arg, expected) in enumerate(test_patterns): with self.subTest(test=i): s = main.Solution() tree: TreeNode = createTreeNode(arg) self.assertEqual(s.pseudoPalindromicPaths(tree), expected)
def test_countNodes(self): test_patterns = [ ([1, 2, 3, 4, 5, 6], 6), ] for i, (arg, expected) in enumerate(test_patterns): with self.subTest(test=i): s = main.Solution() tree: TreeNode = createTreeNode(arg) self.assertEqual(s.countNodes(tree), expected)
def test_invertTreee(self): test_patterns = [ ([4, 2, 7, 1, 3, 6, 9], [4, 7, 2, 9, 6, 3, 1]), ] for i, (arg, expected) in enumerate(test_patterns): with self.subTest(test=i): s = main.Solution() tree: TreeNode = createTreeNode(arg) self.assertEqual(getTreeNode(s.invertTree(tree)), expected)
def test_zigzagLevelOrder(self): test_patterns = [ ([3, 9, 20, 10, 2, 15, 7], [[3], [20, 9], [10, 2, 15, 7]]), ] for i, (arg, expected) in enumerate(test_patterns): with self.subTest(test=i): s = main.Solution() tree: TreeNode = createTreeNode(arg) self.assertEqual(s.zigzagLevelOrder(tree), expected)
def test_diameterOfBinaryTree(self): test_patterns = [ ([1, 2, 3, 4, 5, 6, 7], 4), ] for i, (arg, expected) in enumerate(test_patterns): with self.subTest(test=i): s = main.Solution() tree: TreeNode = createTreeNode(arg) self.assertEqual(s.diameterOfBinaryTree(tree), expected)
def test_levelOrderBottom(self): test_patterns = [ ([1, 2, 3, 4, 5, 6, 7], [[4, 5, 6, 7], [2, 3], [1]]), ] for i, (arg, expected) in enumerate(test_patterns): with self.subTest(test=i): s = main.Solution() tree: TreeNode = createTreeNode(arg) self.assertEqual(s.levelOrderBottom(tree), expected)
def test_maxPathSum(self): test_patterns = [ ([-10, 9, 20, 0, 0, 15, 7], 42), ] for i, (arg, expected) in enumerate(test_patterns): with self.subTest(test=i): s = main.Solution() tree: TreeNode = createTreeNode(arg) self.assertEqual(s.maxPathSum(tree), expected)
def test_searchBST(self): test_patterns = [ ([4, 2, 7, 1, 3, 6, 10], 2, [2, 1, 3]) ] for i, (arg1, arg2, expected) in enumerate(test_patterns): with self.subTest(test=i): s = main.Solution() tree: TreeNode = createTreeNode(arg1) self.assertEqual(getTreeNode(s.searchBST(tree, arg2)), expected)
def test_buildTree(self): test_patterns = [ ([11, 9, 14, 3, 15, 20, 7], [11, 14, 9, 15, 7, 20, 3], [3, 9, 20, 11, 14, 15, 7]), ] for i, (arg1, arg2, expected) in enumerate(test_patterns): with self.subTest(test=i): s = main.Solution() self.assertEqual(getTreeNode(s.buildTree(arg1, arg2)), getTreeNode(createTreeNode(expected)))
def test_isCousins(self): test_patterns = [ ([1, 2, 3, 6, 7, 4, 5], 6, 5, True), ([1, 2, 3, 6, 7, 4, 5], 4, 5, False), ] for i, (arg1, arg2, arg3, expected) in enumerate(test_patterns): with self.subTest(test=i): s = main.Solution() tree: TreeNode = createTreeNode(arg1) self.assertEqual(s.isCousins(tree, arg2, arg3), expected)
def test_kthSmallest(self): test_patterns = [ ([4, 2, 6, 1, 3, 5, 7], 1, 1), ([4, 2, 6, 1, 3, 5, 7], 3, 3), ] for i, (arg1, arg2, expected) in enumerate(test_patterns): with self.subTest(test=i): s = main.Solution() tree: TreeNode = createTreeNode(arg1) self.assertEqual(s.kthSmallest(tree, arg2), expected)
def test_isValidSequence(self): test_patterns = [ ([0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], [0, 1, 0, 1], True), ([0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], [0, 1, 0], False), ] for i, (arg1, arg2, expected) in enumerate(test_patterns): with self.subTest(test=i): s = main.Solution() tree: TreeNode = createTreeNode(arg1) self.assertEqual(s.isValidSequence(tree, arg2), expected)
def test_widthOfBinaryTree(self): test_patterns = [ ([0, 0, 0, 0, None, None, 0, None, None, None, 0], 4), ([1, 3, 2, 5, 3, None, 9], 4), ([0], 1), ] for i, (arg, expected) in enumerate(test_patterns): with self.subTest(test=i): s = main.Solution() tree: TreeNode = createTreeNode(arg) self.assertEqual(s.widthOfBinaryTree(tree), expected)