class BiTNode:
def __init__(self):
self.data=None
self.lchild=None
self.rchild=None
class Test:
def __init__(self):
self.pHead=None
self.pEnd=None
def arrayToTree(self,arr,start,end):
root=None
if start<=end:
root=BiTNode()
mid=(start+end+1)/2
root.data=arr[mid]
root.lchild=self.arrayToTree(arr,start,mid-1)
root.rchild=self.arrayToTree(arr,mid+1,end)
else:
root=None
return root
def treeToLinkedList(self,root):
if root==None:
return
self.treeToLinkedList(root.lchild)
root.lchild=self.pEnd
if self.pEnd==None:
self.pHead=root
else:
self.pEnd.rchild=root
self.pEnd=root
self.treeToLinkedList(root.rchild)
if __name__=="__main__":
arr=[1,2,3,4,5,6,7,8]
test=Test()
root=test.arrayToTree(arr,0,len(arr)-1)
test.treeToLinkedList(root)
cur=test.pHead
while cur!=None:
print cur.data,
cur=cur.rchild
print '\n'
cur=test.pEnd
while cur!=None:
print cur.data,
cur=cur.lchild
剑指offer第36题二叉搜索树与双向链表---------python
最新推荐文章于 2021-12-08 20:58:36 发布