| 考点 | 难度 |
|---|---|
| Tree | Medium |
Given the root of a binary tree, flatten the tree into a “linked list”:
The “linked list” should use the same TreeNode class where the right child pointer points to the next node in the list and the left child pointer is always null.
The “linked list” should be in the same order as a pre-order traversal of the binary tree.
如果curr有left child, 把right subtree移到左边:

1 先找到left subtree右边分叉的最下面一个node p
2 把curr.right移动到p.right
3 把curr.left移到curr.left
class Solution:
def flatten(self, root: TreeNode) -> None:
curr = root
while curr:
if curr.left != None:
p = curr.left
while p.right != None:
p = p.right
p.right = curr.right
curr.right = curr.left
curr.left = None
curr = curr.right