Exemplo n.º 1
0
 def bst_2(cls, negtive=True):
     if negtive:
         root = TreeNode(0)
         root.right = TreeNode(-1)
     else:
         root = TreeNode(1)
         root.right = TreeNode(3)
         root.right.left = TreeNode(2)
     return root
Exemplo n.º 2
0
 def convert(left, right):
     if left > right: return None
     mid = (left + right) // 2
     node = TreeNode(nums[mid])
     node.left = convert(left, mid - 1)
     node.right = convert(mid + 1, right)
     return node
Exemplo n.º 3
0
 def bst_230(cls):
     """
        3
       / \
      1   4
       \
        2
     """
     root = TreeNode(3)
     root.left = TreeNode(1)
     root.right = TreeNode(4)
     root.left.right = TreeNode(2)
     return root
Exemplo n.º 4
0
 def bst_1(cls, negtive=True):
     """
             2
            / \
           1   3
     """
     if negtive:
         root = TreeNode(2)
         root.left = TreeNode(1)
         root.right = TreeNode(3)
     else:
         root = TreeNode(1)
         root.left = TreeNode(1)
     return root
Exemplo n.º 5
0
 def bst_501(cls, extend=False):
     """ 4、3 都可切成 5
        4
      /  \
     2    6
    / \  / \
   1  3  5  6
     """
     if not extend:
         root = TreeNode(1)
         root.right = TreeNode(2)
         root.right.left = TreeNode(2)
         return root
     else:
         root = TreeNode(5)
         root.left, root.right = TreeNode(2), TreeNode(6)
         root.left.left, root.left.right = TreeNode(1), TreeNode(3)
         root.right.left, root.right.right = TreeNode(5), TreeNode(6)
         return root