Description
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Example: Given the sorted linked list: [-10,-3,0,5,9], One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:
0
/ \
-3 9
/ /
-10 5
想法:
和108 Convert Sorted Array to Binary Search Tree 相似, 但是List無法直接透過index查找,概念一樣是從中間切一半,用快慢指針找到中間點,並要記錄slow前面一個node, 找到中點後以中點建立根節點root,接著把List斷開,左右兩邊的List都不包含中點,在對兩邊做遞迴 讓root連上左右兩邊 因為左邊部分做到最後會是slow == head,要另外設條件說當recursion做到這裡後,要停 止,右邊因為base case已經有 not head的狀況,所以不用額外再處理了
Code:
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def sortedListToBST(self, head):
"""
:type head: ListNode
:rtype: TreeNode
"""
if not head: return None
if not head.next: return TreeNode(head.val)
# 用快慢指針找到中點並切斷分為左右子樹
prev = slow = fast = head
while fast and fast.next:
prev = slow
slow = slow.next
fast = fast.next.next
second = slow.next
prev.next = None
current = TreeNode(slow.val)
# if head != slow: # head == slow: 表示左邊沒了
current.left = self.sortedListToBST(head)
current.right = self.sortedListToBST(second)
return current