15min,除了忘了防空,一遍通过。主要还是在脑袋里跑了好久。
class Solution:
def __init__(self):
self.end=None
def Convert(self, pRootOfTree):
if not pRootOfTree:return None # 忘了防空
self.dfs(pRootOfTree)
cur=self.end
while cur.left:
cur=cur.left
return cur
def dfs(self,pRoot:TreeNode):
if not pRoot:return
self.dfs(pRoot.left)
pRoot.left=self.end
if self.end:self.end.right=pRoot
self.end=pRoot
self.dfs(pRoot.right)