114.二叉树展开为链表
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def flatten(self, root: TreeNode) -> None:
"""
Do not return anything, modify root in-place instead.
"""
return self.dfs(root,None)
def dfs(self,root,Node):
if not root : return Node
# 记录当前遍历节点的前一个节点
# 首先将右子树转换完成,然后将转换后的链表与左子树最后一层的
# 最右边的节点
Node = self.dfs(root.right,Node)
Node = self.dfs(root.left,Node)
# 拼接
root.right = Node
root.left = None
return root