# 114. 二叉树展开为链表

## 题目

``````    1
/ \
2   5
/ \   \
3   4   6``````

``````1
\
2
\
3
\
4
\
5
\
6``````

## 解题思路

### 递归

• 先找到左子树的最右节点；
• 当找到左子树的最右节点时，令其指向根的右子树；
• 此时，再让根的右子树，指向根节点的左子树；
• 最后，令根节点的左子树为 None，循环直至右子树为空。

## 代码实现

``````# 递归
# Definition for a binary tree node.
# 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.
"""
def unflod(node):
if not node:
return None

unflod(node.left)
unflod(node.right)

# 后序遍历
if node.left:
pre = node.left
# 找到左子树的最右子节点，用以连接根的右子树
while pre.right:
pre = pre.right
# 当找到左子树的最右子节点，令其指向根的右子树
pre.right = node.right
# 然后令根的右子树指向根的左子树，令根的左子树为空
node.right = node.left
node.left = None
# 循环
node = node.right

unflod(root)

# 非递归
# Definition for a binary tree node.
# 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.
"""
while root:
if root.left:
pre_node = root.left
# 同样先找到左子树的最右节点
while pre_node.right:
pre_node = pre_node.right
# 最右节点指向根节点的右子树
pre_node.right = root.right
# 根的右子树指向根的左子树，同时置空左子树
root.right = root.left
root.left = None
root = root.right``````