# 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); } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up