MCPcopy Create free account
hub / github.com/HuberTRoy/leetCode / Solution

Class Solution

Tree/ConvertSortedListToBinarySearchTree.py:114–141  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

112# self.right = None
113
114class Solution(object):
115 def sortedListToBST(self, head):
116 """
117 :type head: ListNode
118 :rtype: TreeNode
119 """
120 size = 0
121
122 self.c_head = head
123
124 while head:
125 size += 1
126 head = head.next
127
128 def makeBSTByInorder(size):
129 if not size:
130 return
131
132 root = TreeNode(None)
133
134 root.left = makeBSTByInorder(size//2)
135 root.val = self.c_head.val
136 self.c_head = self.c_head.next
137 root.right = makeBSTByInorder(size-size//2-1)
138
139 return root
140
141 return makeBSTByInorder(size)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected