0114. Flatten Binary Tree to Linked List
Last updated
Last updated
**Input:** root = [1,2,5,3,4,null,6]
**Output:** [1,null,2,null,3,null,4,null,5,null,6]**Input:** root = []
**Output:** []**Input:** root = [0]
**Output:** [0]class Solution {
public void flatten(TreeNode root) {
if (root == null || (root.left == null && root.right == null)) return;
flatten(root.left);
flatten(root.right);
TreeNode tmpR = root.right;
TreeNode tmpL = root.left;
root.left = null;
root.right = tmpL;
while (tmpL.right != null) tmpL = tmpL.right;
tmpL.right = tmpR;
return;
}
}class Solution {
private TreeNode tmp = null;
public void flatten(TreeNode root) {
if (root == null) return;
flatten(root.right);
flatten(root.left);
root.left = null;
root.right = tmp;
tmp = root;
}
}