def _add(value, node):
     if value < node.val:
         if not node.left:
             node.left = TreeNode(value)
         else:
             _add(value, node.left)
     else:
         if not node.right:
             node.right = TreeNode(value)
         else:
             _add(value, node.right)
Esempio n. 2
0
    def test_validBinaryTreeIsValid(self):

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

        self.assertTrue(is_bst(root))
Esempio n. 3
0
 def remove_by_recursion(self, value):
     if not self.root:
         return False
     elif value == self.root.data:
         temp_root = TreeNode()      # 创建一个临时节点 方便处理
         temp_root.left = self.root
         ret = BinarySearchTree.__remove__(temp_root.left, temp_root, value)
         self.root = temp_root.left
         return ret
     elif value < self.root.data and self.root.left:
         return BinarySearchTree.__remove__(self.root.left, self.root, value)
     elif value > self.root.data and self.root.right:
         return BinarySearchTree.__remove__(self.root.right, self.root, value)
     return False
Esempio n. 4
0
 def __add_value(node, value):
     if not node:
         return False
     if node.data == value:
         return False       # duplicate
     elif value < node.data:
         if node.left:
             return BinarySearchTree.__add_value(node.left, value)
         else:
             node.left = TreeNode(value)
     else:
         if node.right:
             return BinarySearchTree.__add_value(node.right, value)
         else:
             node.right = TreeNode(value)
     return True
Esempio n. 5
0
    def test_invalidTreeIsNotBST(self):

        root = TreeNode(20)
        root.left = TreeNode(10)
        root.right = TreeNode(30)
        root.right.left = TreeNode(5)
        root.right.right = TreeNode(40)

        self.assertFalse(is_bst(root))
Esempio n. 6
0
    def setUp(self):
        nodes = {}
        for i in range(8):
            nodes[i] = TreeNode(i)

        self.root = nodes[1]
        nodes[1].left_child = nodes[2]
        nodes[1].right_child = nodes[3]
        nodes[2].left_child = nodes[4]
        nodes[2].right_child = nodes[5]
        nodes[3].left_child = nodes[6]
        nodes[3].right_child = nodes[7]
def build_bst(preorder: list) -> TreeNode:
    if not preorder:
        return TreeNode("")

    head = TreeNode(preorder[0])

    def _add(value, node):
        if value < node.val:
            if not node.left:
                node.left = TreeNode(value)
            else:
                _add(value, node.left)
        else:
            if not node.right:
                node.right = TreeNode(value)
            else:
                _add(value, node.right)

    for value in preorder[1:]:
        _add(value, head)

    return head
    def test_lowest_common_ancestor_recursive(self):
        nodes = {}
        for i in range(1, 8):
            nodes[i] = TreeNode(i)

        root = nodes[1]
        nodes[1].left_child = nodes[2]
        nodes[1].right_child = nodes[3]
        nodes[2].left_child = nodes[4]
        nodes[2].right_child = nodes[5]
        nodes[3].left_child = nodes[6]
        nodes[3].right_child = nodes[7]

        a = nodes[6]
        b = nodes[4]
        e = nodes[1]
        self.assertEqual(BinaryTree().lowest_common_ancestor_recursive(root, a, b), e)

        a = nodes[4]
        b = nodes[5]
        e = nodes[2]
        self.assertEqual(BinaryTree().lowest_common_ancestor_recursive(root, a, b), e)
 def test_tree_root_only(self):
     root = TreeNode()
     root.build_from_heap([1])
     self.assertEqual(diameter_binary_tree(root), 0)
 def test_tree2(self):
     root = TreeNode()
     root.build_from_heap([1, 2, 3])
     self.assertEqual(diameter_binary_tree(root), 2)
 def test_tree_long_left(self):
     root = TreeNode()
     root.build_from_heap([2, 3, None, 1])
     self.assertEqual(diameter_binary_tree(root), 2)
 def test_tree1432(self):
     root = TreeNode()
     root.build_from_heap([1, 4, 3, 2])
     self.assertEqual(diameter_binary_tree(root), 3)
 def test_minus(self):
     root = TreeNode()
     root.build_from_heap([-10, 9, 20, None, None, 15, 7])
     solution = Solution()
     self.assertEqual(solution.maxPathSum(root), 42)
 def test_85171012(self):
     test_head = TreeNode()
     test_head.build_from_heap([8, 5, 10, 1, 7, None, 12])
     self.assertEqual(build_bst([8, 5, 1, 7, 10, 12]), test_head)
Esempio n. 15
0
 def test_0101(self):
     root = TreeNode()
     root = root.build([0, 1, 0, 0, 1, 0, None, None, 1, 0, 0], root, 0)
     self.assertTrue(sequence_in_tree(root, [0, 1, 0, 1]))
Esempio n. 16
0
 def test_29180(self):
     root = TreeNode()
     root = root.build([2, 9, 3, None, 1, None, 2, None, None, None, 8],
                       root, 0)
     self.assertFalse(sequence_in_tree(root, [2, 9, 1, 8, 0]))
Esempio n. 17
0
 def add(self, value):
     if not self.root:
         self.root = TreeNode(value)
         return True
     else:
         return BinarySearchTree.__add_value(self.root, value)
Esempio n. 18
0
 def test_0035(self):
     root = TreeNode()
     root = root.build([0, 0, 1, 3, 4, 2, None, 5], root, 0)
     self.assertTrue(sequence_in_tree(root, [0, 0, 3, 5]))
Esempio n. 19
0
        return

    list = None
    if len(lists) == level:
        list = []
        list.append(root)
        lists.append([])
    else:
        list = lists[level]
        list.append(root)

    createLevelLinkedList(root.left, level + 1)
    createLevelLinkedList(root.right, level + 1)


ten = TreeNode(10)

eight = TreeNode(8)
eight.left = TreeNode(6)
eight.right = TreeNode(9)

twelve = TreeNode(12)

eight.left
ten.left = eight
ten.right = twelve

sol = createLevelLinkedList(ten, 0)
for i in lists:
    print(i)
Esempio n. 20
0
 def test_011(self):
     root = TreeNode()
     root = root.build([0, 1, 0, 0, 1, 0, 8, 8, 1, 0, 0], root, 0)
     self.assertFalse(sequence_in_tree(root, [0, 1, 1]))
 def test_123(self):
     root = TreeNode()
     root.build_from_heap([1, 2, 3])
     solution = Solution()
     self.assertEqual(solution.maxPathSum(root), 6)