-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path109. 有序链表转换二叉搜索树.py
46 lines (42 loc) · 1.1 KB
/
109. 有序链表转换二叉搜索树.py
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
36
37
38
39
40
41
42
43
44
45
46
# -*- encoding: utf-8 -*-
'''
@File : 109. 有序链表转换二叉搜索树.py
@Time : 2020/04/27 16:42:29
@Author : windmzx
@Version : 1.0
@Desc : For leetcode template
'''
# here put the import lib
from typing import List
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def sortedListToBST(self, head: ListNode) -> TreeNode:
if head==None:
return None
if head.next==None:
return TreeNode(head.val)
pre=head
slow=head.next
fast=head.next.next
while fast and fast.next:
pre=pre.next
slow=slow.next
fast=fast.next.next
pre.next=None
root=TreeNode(slow.val)
root.left=self.sortedListToBST(head)
root.right=self.sortedListToBST(slow.next)
return root
if __name__ == "__main__":
x=Solution()
print()