Beispiel #1
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
Beispiel #2
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
Beispiel #3
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
Beispiel #4
0
 def insert(self, root, val):
     if not root: return TreeNode(val)
     if val <= root.val:
         root.left = self.insert(root.left, val)
     else:
         root.right = self.insert(root.right, val)
     return root
Beispiel #5
0
 def bst_99(cls, correct=False):
     """
        1
       /
      3
       \
        2
     """
     if correct:
         root = TreeNode(3)
         root.left = TreeNode(1)
         root.left.right = TreeNode(2)
     else:
         root = TreeNode(1)
         root.left = TreeNode(3)
         root.left.right = TreeNode(2)
     return root
Beispiel #6
0
 def insertIntoBST(self, root, val):
     """
     :type root: TreeNode
     :type val: int
     :rtype: TreeNode
     """
     if not root: return TreeNode(val)
     if root.val < val:
         root.right = self.insertIntoBST(root.right, val)
     else:
         root.left = self.insertIntoBST(root.left, val)
     return root
Beispiel #7
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
Beispiel #8
0
    def recoverTree_DFS(self, root):
        def inorder(node):
            if not node: return
            inorder(node.left)
            if self.pre.val > node.val:
                if not self.first:
                    self.first = self.pre  # 记录第一次出错处, 记录较大值,
                self.second = node  # 记录较小值, 如果当前结点不是最终swap出错处, 则会更新结束
            self.pre = node  # 更新当前极小值结点, 目的是找到最终swap结点
            inorder(node.right)

        self.pre = TreeNode(-sys.maxsize - 1)
        self.first, self.second = None, None
        inorder(root)
        self.first.val, self.second.val = self.second.val, self.first.val
Beispiel #9
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