Example #1
0
    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)
Example #2
0
    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)
Example #3
0
    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)
Example #4
0
    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)
Example #5
0
    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)
Example #6
0
    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)
Example #7
0
    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)
Example #8
0
    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)
Example #9
0
    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)
Example #10
0
    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)
Example #11
0
    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)))
Example #12
0
    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)
Example #13
0
    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)
Example #14
0
    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)
Example #15
0
    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)