标签:nod 节点 turn init flat root dfs instead att
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
标签:nod 节点 turn init flat root dfs instead att
原文地址:https://www.cnblogs.com/cong12586/p/13170205.html