Exemplo n.º 1
0
 def test_two_medium_and_different_binary_trees(self):
     tree_p = TreeNode(1,
                       left=TreeNode(2, left=TreeNode(4)),
                       right=TreeNode(4))
     tree_q = TreeNode(1,
                       left=TreeNode(2, left=TreeNode(3)),
                       right=TreeNode(3))
     self.assertFalse(Tree().is_same_tree(tree_p, tree_q))
Exemplo n.º 2
0
 def test_two_medium_and_identical_binary_trees(self):
     tree_p = TreeNode(1,
                       left=TreeNode(2, left=TreeNode(3)),
                       right=TreeNode(4))
     tree_q = TreeNode(1,
                       left=TreeNode(2, left=TreeNode(3)),
                       right=TreeNode(4))
     self.assertTrue(Tree().is_same_tree(tree_p, tree_q))
Exemplo n.º 3
0
 def test_two_null_tree(self):
     p = TreeNode(val=None)
     q = TreeNode(val=None)
     self.assertTrue(Tree().is_same_tree(p, q))
Exemplo n.º 4
0
 def test_two_large_and_slightly_different_binary_trees(self):
     tree_p = TreeNode(1,
                       left=TreeNode(7),
                       right=TreeNode(6,
                                      left=TreeNode(8),
                                      right=TreeNode(9, left=TreeNode(9))))
     tree_q = TreeNode(1,
                       left=TreeNode(7),
                       right=TreeNode(6,
                                      left=TreeNode(8),
                                      right=TreeNode(4, left=TreeNode(4))))
     self.assertFalse(Tree().is_same_tree(tree_p, tree_q))
Exemplo n.º 5
0
 def test_two_large_and_identical_binary_trees(self):
     tree_p = TreeNode(1,
                       left=TreeNode(7),
                       right=TreeNode(6,
                                      left=TreeNode(8),
                                      right=TreeNode(4, left=TreeNode(9))))
     tree_q = TreeNode(1,
                       left=TreeNode(7),
                       right=TreeNode(6,
                                      left=TreeNode(8),
                                      right=TreeNode(4, left=TreeNode(9))))
     self.assertTrue(Tree().is_same_tree(tree_p, tree_q))
Exemplo n.º 6
0
 def test_two_simple_and_different_binary_trees(self):
     tree_p = TreeNode(1, left=TreeNode(2))
     tree_q = TreeNode(1, right=TreeNode(2))
     self.assertFalse(Tree().is_same_tree(tree_p, tree_q))
Exemplo n.º 7
0
 def test_two_simple_and_identical_binary_trees(self):
     tree_p = TreeNode(1)
     tree_q = TreeNode(1)
     self.assertTrue(Tree().is_same_tree(tree_p, tree_q))
	if val <= lower_val or val >= higher_val:
		print("we in the weird conditional")
		return False

	# recursivelt check if child mode is higher than
	# parent, pass parent in place of lower value
	if not is_valid_bst(node.right,val,higher_val):
		return False

	# recursively check if left child node is less than 
	# parent, pass parent in place of higher value
	if not is_valid_bst(node.left,lower_val,val):
		return False

	return True

t = TreeNode(5)

t.left = TreeNode(2)
t.right = TreeNode(8)

t.left.left = TreeNode(1)
t.left.right = TreeNode(4)

t.right.left = TreeNode(6)
t.right.right = TreeNode(9)


print("t is valid: ",is_valid_bst(t))

    def create_levels(node, level):

        if level == len(levels):
            # start a new level as empty list
            levels.append([])

        # add value to nested list
        levels[level].append(node.data)

        # recursively call left and right children
        # and add to level
        if node.left:
            create_levels(node.left, level + 1)

        if node.right:
            create_levels(node.right, level + 1)

    create_levels(tree, 0)
    return levels


tree_one = TreeNode(1)
tree_one.right = TreeNode(3)
tree_one.left = TreeNode(2)
tree_one.left.left = TreeNode(4)
tree_one.left.right = TreeNode(5)
tree_one.right.left = TreeNode(6)
tree_one.right.right = TreeNode(7)

print(print_tree_levels(tree_one))