def test_path_sum(self):
        tree = tn(0, 0)
        tree.left = tn(0, -1)
        tree.left.left = tn(0, 2)
        tree.left.right = tn(0, 3)

        self.assertEqual(1, self.fixture.path_sum(tree, 1, 0))
 def test_first_common_ancestor(self):
     self.root.left.left = tn(0, 5)
     self.root.left.right = tn(0, 5)
     self.root.right.right = tn(0, 5)
     self.assertEqual(
         1,
         self.fixture.first_common_ancestor(self.root, self.root.left.left,
                                            self.root.right.right).val)
 def test_list_of_depths(self):
     array = [1, 2, 3, 4, 5, 6, 7, 8]
     input = self.fixture.create_minimal_bst_tree(array, 0, len(array) - 1)
     actual = self.fixture.list_of_depths(input)
     self.assertEqual([4],
                      self.fixture.list_of_depths(tn(0, 4))[0].getNodes(
                          list()))
     self.assertEqual([4], actual[0].head.getNodes(list()))
     self.assertEqual([2, 6], actual[1].head.getNodes(list()))
     self.assertEqual([1, 3, 5, 7], actual[2].head.getNodes(list()))
     self.assertEqual([8], actual[3].head.getNodes(list()))
    def test_flip_equilvalent(self):
        #[0,3,1,None,None,None,2]
        root = tn(0)
        root.left = tn(3)
        root.right = tn(1)
        root.right.right = tn(2)

        #[0,3,1,2]
        root1 = tn(0)
        root1.left = tn(3)
        root1.right = tn(1)
        root1.left.left = tn(2)
        self.assertTrue(self.fixture.flipEquiv(root, root1))
    def test_path_sum_result(self):
        tree = tn(0, 0)
        tree.left = tn(0, -1)
        tree.left.left = tn(0, 2)
        tree.left.right = tn(0, 3)
        tree.right = tn(0, 1)
        tree.right.left = tn(0, 1)
        tree.right.right = tn(0, 2)

        result = []
        self.assertEqual([[0, -1, 2], [0, 1]],
                         self.fixture.path_sum_result(tree, 1, 0, result, []))
    def test_even_sum(self):
        tree = tn(0, 10)
        tree.left = tn(0, 2)
        tree.left.left = tn(0, 1)
        tree.left.right = tn(0, 101)
        tree.right = tn(0, 5)
        tree.right.right = tn(0, 13)

        print(self.fixture.even_sum(tree))
    def test_check_subtree(self):
        t1 = tn(0, 0)
        t1.left = tn(0, 1)
        t1.left.left = tn(0, 2)
        t1.left.right = tn(0, 3)

        t2 = tn(0, 1)
        t2.left = tn(0, 2)
        t2.right = tn(0, 3)
        self.assertTrue(self.fixture.check_subtree(t1, t2))

        t1.left.left = None
        self.assertFalse(self.fixture.check_subtree(t1, t2))
 def test_is_not_bst(self):
     self.fixture.root = tn(0, 3)
     self.fixture.root.left = tn(0, 2)
     self.fixture.root.right = tn(0, 2)
     self.assertFalse(self.fixture.is_bst(self.fixture.root))
 def test_depth(self):
     input = [1, 2, 3, 4, 5, 6, 7, 8]
     tree = self.fixture.create_minimal_bst_tree(input, 0, len(input) - 1)
     self.assertEqual(0, self.fixture.level(None))
     self.assertEqual(1, self.fixture.level(tn(0, 1)))
     self.assertEqual(4, self.fixture.level(tree))