[LeetCode] Flatten Binary Tree to Linked List, leetcodeflatten
Title: Flatten Binary Tree to Linked List
<Span style = "font-size: 18px;">/** LeetCode Flatten Binary Tree to Linked List *: Given a Binary Tree, transform it into a structure equivalent to a single-chain table. We can see that this structure is: The left son of each node is empty, and the right son points to the next node in the previous order * idea: when observed, we can get the correct sequence connected by * Definition for binary tree * public class TreeNode {* int val; * TreeNode left; * TreeNode right; * TreeNode (int x) {val = x;} */package javaTrain; public class Train18 {public void flatten (TreeNode root) {if (root = Null | (root. left = null & root. right = null) return; preOrder (root); return;} private TreeNode preOrder (TreeNode root) {if (root = null | (root. left = null & root. right = null) return root; TreeNode left = root. left; TreeNode right = root. right; TreeNode last; root. left = null; if (left! = Null) {root. right = left; last = preOrder (left); last. left = null; if (right! = Null) {last. right = right; return preOrder (right);} else return last;} else {root. right = right; return preOrder (right) ;}}</span>