题目描述
方法一:
递归实现
class Solution:
def __init__(self):
self.realHead=None
self.head=None
def Convert(self, pRootOfTree):
# write code here
if not pRootOfTree:
return None
self.Convert(pRootOfTree.left)
if not self.realHead:
self.realHead=pRootOfTree
self.head=self.realHead
else:
pRootOfTree.left=self.head
self.head.right=pRootOfTree
self.head=pRootOfTree
self.Convert(pRootOfTree.right)
return self.realHead
方法二:
不用递归实现
class Solution:
if pRootOfTree is None:
return None
p=pRootOfTree
s=[]
isfirt=True
while p or s:
if p:
s.append(p)
p=p.left
else:
p=s.pop()
if isfirt:
root=p
prep=p
isfirt=False
else:
prep.right=p
p.left=prep
prep=p
p=p.right
return root