1
這個想法是非常相似的其他大多數人,做一個inorder到位遍歷治療左prevNode和右nextNode 因爲某些原因,它只是不能工作..似乎沒有運行遞歸?蟒蛇轉換排序雙鏈表到BST
我測試了我的DoubleLinked名單是由印刷preNode和nextNode
正確池莉構建,但仍說「NoneType」對象有沒有屬性「VAL」 的問題是buildTree
任何一個plz幫助
class LinkNode(object):
def __init__(self, val):
self.val = val
self.nextNode = None
self.prevNode = None
def buildLinkList(arr):
dummy = head = LinkNode(None)
dummy.nextNode = head
for val in arr:
new_node = LinkNode(val)
new_node.prevNode = head
head.nextNode = new_node
head = head.nextNode
return dummy.nextNode
def printLink(head):
while head:
print head.val
if not head.nextNode:
#print head.val
return head
head = head.nextNode
def buildTree(head, n):
if n <= 0:
return None
left = buildTree(head, n/2)
print head.val
root = head
root.prevNode = left
head = head.nextNode
root.nextNode = buildTree(head, n - n/2 - 1)
return root
def inorder(root):
if root:
inorder(root.prevNode)
print root.val
inorder(root.nextNode)
arr = [1, 2, 3, 4, 5, 6, 7]
head = buildLinkList(arr)
#print head.val
root = buildTree(head, 7)