コード例 #1
0
    def sortedArrayToBST(self, nums):
        """
        :type nums: List[int]
        :rtype: TreeNode
        """
        if not nums:
            return None

        mid = len(nums) // 2
        root = TreeNode(nums[mid])
        root.left = self.sortedArrayToBST(nums[:mid])
        root.right = self.sortedArrayToBST(nums[mid + 1:])
        return root
コード例 #2
0
 def flatten(self, root):
     """
     :type root: TreeNode
     :rtype: void Do not return anything, modify root in-place instead.
     """
     if not root:
         return
     self.arr = []
     self.preorder(root)
     pre = TreeNode(0)
     for node in self.arr:
         pre.left = None
         pre.right = node
         pre = node
コード例 #3
0
    def buildTree(self, inorder, postorder):
        """
        :type inorder: List[int]
        :type postorder: List[int]
        :rtype: TreeNode
        """
        if not inorder:
            return None

        val = postorder.pop()
        index = inorder.index(val)

        root = TreeNode(val)
        root.right = self.buildTree(inorder[index + 1:], postorder)
        root.left = self.buildTree(inorder[:index], postorder)
        return root
コード例 #4
0
    def recoverTree(self, root):
        """
        :type root: TreeNode
        :rtype: void Do not return anything, modify root in-place instead.
        """
        if not root:
            return

        prev = TreeNode(float('-inf'))
        n1, n2 = None, None
        stack = []

        node = root
        while node:
            stack.append(node)
            node = node.left

        while stack:
            node = stack.pop()
            if prev.val > node.val:
                if not n1:
                    n1 = prev
                n2 = node

            prev = node
            node = node.right
            while node:
                stack.append(node)
                node = node.left

        n1.val, n2.val = n2.val, n1.val
コード例 #5
0
    def genBST(self, left, right):
        arr = []
        if left > right:
            arr += None,
            return arr

        for i in range(left, right + 1):
            leftTree = self.genBST(left, i - 1)
            rightTree = self.genBST(i + 1, right)
            for lTree in leftTree:
                for rTree in rightTree:
                    root = TreeNode(i)
                    root.left = lTree
                    root.right = rTree
                    arr += root,
        return arr
コード例 #6
0
    def addOneRow(self, root, v, d):
        """
        :type root: TreeNode
        :type v: int
        :type d: int
        :rtype: TreeNode
        """
        if d == 1:
            node = TreeNode(v)
            node.left = root
            return node

        queue = [root]
        layer = 2
        while layer < d:
            size = len(queue)
            for _ in range(size):
                node = queue.pop(0)
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
            layer += 1

        for node in queue:
            t1 = node.left
            t2 = node.right
            node.left = TreeNode(v)
            node.right = TreeNode(v)
            node.left.left = t1
            node.right.right = t2

        return root
コード例 #7
0
 def recoverTree(self, root):
     """
     :type root: TreeNode
     :rtype: void Do not return anything, modify root in-place instead.
     """
     self.prev = TreeNode(float('-inf'))
     self.res = [None, None]
     self.inorder(root)
     self.res[0].val, self.res[1].val = self.res[1].val, self.res[0].val
コード例 #8
0
    def sortedListToBST(self, head):
        """
        :type head: ListNode
        :rtype: TreeNode
        """
        if not head:
            return None

        dummy = ListNode(0)
        dummy.next = head

        p1, p2 = dummy, head
        while p2 and p2.next:
            p1 = p1.next
            p2 = p2.next.next

        mid = p1.next
        p1.next = None
        root = TreeNode(mid.val)
        if head != mid:
            root.left = self.sortedListToBST(head)
            root.right = self.sortedListToBST(mid.next)
        return root
コード例 #9
0
class Solution(object):
    def lowestCommonAncestor(self, root, p, q):
        """
        :type root: TreeNode
        :type p: TreeNode
        :type q: TreeNode
        :rtype: TreeNode
        """
        if p.val < root.val < q.val:
            return root
        elif root.val < p.val and root.val < q.val:
            return self.lowestCommonAncestor(root.right, p, q)
        elif root.val > p.val and root.val > q.val:
            return self.lowestCommonAncestor(root.left, p, q)
        return root


if __name__ == "__main__":
    try:
        from utils.TreeNode import createTreeNode, TreeNode
        sol = Solution()
        root = createTreeNode([6, 2, 8, 0, 4, 7, 9, None, None, 3, 5])
        print(
            sol.lowestCommonAncestor(root, TreeNode(3), TreeNode(5)).val == 4)
        print(
            sol.lowestCommonAncestor(root, TreeNode(0), TreeNode(5)).val == 2)
        print(
            sol.lowestCommonAncestor(root, TreeNode(2), TreeNode(8)).val == 6)
    except Exception as e:
        print(e)