---
# System prepended metadata

title: 0114. Flatten Binary Tree to Linked List
tags: [Leetcode, FaceBook, DFS, Medium]

---

# 0114. Flatten Binary Tree to Linked List
###### tags: `Leetcode` `Medium` `FaceBook` `DFS`
Link: https://leetcode.com/problems/flatten-binary-tree-to-linked-list/
## 思路
先preorder遍历全连在左边，然后再全换到右边
## Code
```java=
class Solution {
    TreeNode last = null;
    public void flatten(TreeNode root) {
        preOrder(root);
        if(root==null) return;
        TreeNode curr = root;
        while(curr.left != null){
           TreeNode next = curr.left;
            curr.right = curr.left;
            curr.left = null;
            curr = curr.right;
        }
        return;
    }
    public void preOrder(TreeNode root){
        if(root==null) return;
        if(last == null){
            last = root;
        }
        else if(last!=null){
            last.left = root;
            last = root;
        }
        if(root.left!=null)preOrder(root.left);
        if(root.right!=null)preOrder(root.right);
    }
}
```