forked from yingl/LintCodeInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_sorted_list_to_balanced_bst.py
More file actions
35 lines (33 loc) · 1.08 KB
/
convert_sorted_list_to_balanced_bst.py
File metadata and controls
35 lines (33 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# coding: utf-8
class Solution:
"""
@param head: The first node of linked list.
@return: a tree node
"""
def sortedListToBST(self, head):
# write your code here
if not head:
return None
# 通过两个指针,一个一步一个两步将链表分为前后两段。
first_node, second_node = head, head.next
prev = None
while second_node:
if second_node:
second_node = second_node.next
if second_node:
second_node = second_node.next
prev = first_node
first_node = first_node.next
else:
break
else:
break
new_head = first_node.next
if prev:
prev.next = None
root = TreeNode(first_node.val)
if head != first_node:
root.left = self.sortedListToBST(head)
root.right = self.sortedListToBST(new_head)
return root
# medium: http://lintcode.com/zh-cn/problem/convert-sorted-list-to-balanced-bst/