Binary tree expands into linked list
Title description: Give you the root node root of the binary tree, please expand it into a singly linked list:
- The expanded singly linked list should also use TreeNode, where the right child pointer points to the next node in the linked list, and the left child pointer is always null.
- The expanded singly linked list should be the same as the binary tree traversal order.
Please refer to the official website of LeetCode for examples.
Source: LeetCode
Link: https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/
The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.
Solution 1: Recursion
Use recursive method to solve, the recursive process is as follows:
- If the current root node is null, return directly;
- If the left and right sub-nodes of the current root node are all empty, no adjustment is required, just return directly;
- If the left subtree of the current root node is empty, only the right subtree of the current root node needs to be processed recursively;
- If the right subtree of the current root node is empty, the left subtree of the current root node needs to be processed recursively, and then the left child node of the current root node points to null, and the right child node points to the processed left subtree;
- If the left and right subtrees of the current root node are not empty, the left and right subtrees of the current root node are respectively processed recursively, and then the left child node of the root node points to null, the right child node points to the processed left subtree, and then the left The last node of the subtree points to the processed right subtree.
import com.kaesar.leetcode.TreeNode;
public class LeetCode_114 {
/**
* 递归
*
* @param root
*/
public static void flatten(TreeNode root) {
// 当前根节点为null时,直接返回
if (root == null) {
return;
}
// 当前根节点的左右子节点都为空时,不需要调整,直接返回
if (root.left == null && root.right == null) {
return;
}
if (root.left == null) {
// 当前根节点的左子树为空,则只需要递归处理当前根节点的右子树
flatten(root.right);
return;
}
if (root.right == null) {
// 当前根节点的右子树为空时,则需要递归处理当前根节点的左子树,然后将当前根节点的左子节点指向null,右子节点指向处理后的左子树
TreeNode left = root.left;
flatten(left);
root.right = left;
root.left = null;
return;
}
// 如果当前根节点的左右子树都不为空
TreeNode left = root.left;
// 递归处理左子树,并找到处理后的最后一个节点
flatten(left);
TreeNode leftLast = left;
while (leftLast.right != null) {
leftLast = leftLast.right;
}
TreeNode right = root.right;
// 递归处理右子树
flatten(right);
// 最后将根节点的左子节点指向null,右子节点指向处理后的左子树,然后将左子树的最后一个节点指向处理后的右子树
root.right = left;
leftLast.right = right;
root.left = null;
}
public static void main(String[] args) {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(5);
root.left.left = new TreeNode(3);
root.left.right = new TreeNode(4);
root.right.right = new TreeNode(6);
System.out.println("展开为链表之前");
root.print();
System.out.println("展开为链表之后");
flatten(root);
root.print();
}
}
[Daily Message] Our life is like a roller coaster, full of highs and lows and excitement. We have a lot of experience. We must have a strong psychological development, an open mind, and an enlarged mind.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。