def flatten(self, root):
# write your code here
if root == None:
return root
#用根节点初始化栈
# 然后出栈一个,就入栈右孩子,左孩子 出1入2
stackList = [root]
dummy = TreeNode(-1)
cur = dummy
while len(stackList) != 0:
node = stackList.pop()
cur.right = node
cur = node
if node.right:
stackList.append(node.right)
if node.left:
stackList.append(node.left)
node.left = None
#不要忘记将左儿子标记为 null,否则你可能会得到空间溢出或是时间溢出
return dummy.right
网友评论