建立二叉树
class TreeNode(object):
def __init__(self, val = None):
self.val = val
self.left = None
self.right = None
A, B, C, D, E, F = [TreeNode(x) for x in range(1,7)]
A.left = B
A.right = C
B.left = D
B.right = E
C.left = F
head = A
转双向链表
class Solution():
def __init__(self):
self.listHead = None
self.listTail = None
def Convert(self, root):
if root==None:
return
self.Convert(root.left)
if self.listHead == None:
self.listHead = root
self.listTail = root
else:
self.listTail.right = root
root.left = self.listTail
self.listTail = root
self.Convert(root.right)
return self.listHead
s = Solution()
resdoub = s.Convert(head)
print(resdoub.right.right.right.val)