106. Convert Sorted List to Binary Search Tree

题目

https://www.lintcode.com/problem/convert-sorted-list-to-binary-search-tree/description?_from=ladder&&fromId=2

实现

使用分治法

  1. 先找到链表中点
  2. 将中点的 next 存起来,然后断开 mid.next = None
  3. 再找左边和右边的 Root,拼接起来即可

代码

class ListNode(object):

    def __init__(self, val, next=None):
        self.val = val
        self.next = next


class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None


class Solution:
    """
    @param: head: The first node of linked list.
    @return: a tree node
    """

    def sortedListToBST(self, head):
        result = self.divide_conquer(head)
        return result

    def divide_conquer(self, head):
        if head is None:
            return None
        if head.next is None:
            return TreeNode(head.val)

        mid = self.go_to_mid(head)

        right_head = mid.next
        mid.next = None

        root = TreeNode(right_head.val)
        root.left = self.divide_conquer(head)
        root.right = self.divide_conquer(right_head.next)

        return root

    def go_to_mid(self, head):
        dummy = ListNode(0)
        dummy.next = head

        slow = dummy
        fast = dummy.next

        while fast is not None and fast.next is not None:
            slow = slow.next
            fast = fast.next.next

        return slow

你可能感兴趣的:(106. Convert Sorted List to Binary Search Tree)