友情支持
如果您觉得这个笔记对您有所帮助,看在D瓜哥码这么多字的辛苦上,请友情支持一下,D瓜哥感激不尽,😜
有些打赏的朋友希望可以加个好友,欢迎关注D 瓜哥的微信公众号,这样就可以通过公众号的回复直接给我发信息。
公众号的微信号是: jikerizhi 。因为众所周知的原因,有时图片加载不出来。 如果图片加载不出来可以直接通过搜索微信号来查找我的公众号。 |
114. Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.
For example, given the following tree:
1 / \ 2 5 / \ \ 3 4 6
The flattened tree should look like:
1 \ 2 \ 3 \ 4 \ 5 \ 6
思路分析
这道题本质上来讲可以说是要做一个先根遍历。但是,却可以将这个过程逆向过来,从底向上建立起关联。不可谓不精巧。
思考题:看题解中,可以逐级将左树并入到右树。尝试一下。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Flatten Binary Tree to Linked List.
* Memory Usage: 38.4 MB, less than 40.00% of Java online submissions for Flatten Binary Tree to Linked List.
*
* Copy from: https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by--26/[详细通俗的思路分析,多解法 - 二叉树展开为链表 - 力扣(LeetCode)]
*/
private TreeNode pre;
public void flatten(TreeNode root) {
if (Objects.isNull(root)) {
return;
}
flatten(root.right);
flatten(root.left);
root.right = pre;
root.left = null;
pre = root;
}
/**
* Runtime: 3 ms, faster than 29.55% of Java online submissions for Flatten Binary Tree to Linked List.
* Memory Usage: 39.8 MB, less than 10.91% of Java online submissions for Flatten Binary Tree to Linked List.
*/
public void flattenQueue(TreeNode root) {
Queue<TreeNode> queue = new ArrayDeque<>();
preorder(root, queue);
TreeNode current = queue.poll();
while (!queue.isEmpty()) {
current.right = queue.poll();
current.left = null;
current = current.right;
if (Objects.nonNull(current)) {
current.left = null;
}
}
}
private void preorder(TreeNode root, Queue<TreeNode> queue) {
if (Objects.isNull(root)) {
return;
}
queue.add(root);
preorder(root.left, queue);
preorder(root.right, queue);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* 参考 https://leetcode.cn/problems/flatten-binary-tree-to-linked-list/solutions/17274/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by--26/[114. 二叉树展开为链表^] 的解法,自己实现的。
*/
public void flatten(TreeNode root) {
if (root == null) {
return;
}
while (root != null) {
if (root.left == null) {
root = root.right;
} else {
TreeNode pre = root.left;
while (pre.right != null) {
pre = pre.right;
}
pre.right = root.right;
root.right = root.left;
root.left = null;
root = root.right;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* @author D瓜哥 · https://www.diguage.com
* @since 2024-07-06 17:12:19
*/
public void flatten(TreeNode root) {
if (root == null) {
return;
}
while (root != null) {
if (root.left != null) {
TreeNode mostRight = root.left;
while (mostRight.right != null) {
mostRight = mostRight.right;
}
mostRight.right = root.right;
root.right = root.left;
root.left = null;
}
root = root.right;
}
}
思考题
如何使用递归来实现?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
* 递归解决方案
*
* @author D瓜哥 · https://www.diguage.com
* @since 2024-07-06 17:32:56
*/
public void flatten(TreeNode root) {
if (root == null) {
return;
}
flatten(root.left);
flatten(root.right);
TreeNode left = root.left;
TreeNode right = root.right;
root.left = null;
root.right = left;
TreeNode mostRight = root;
while (mostRight.right != null) {
mostRight = mostRight.right;
}
mostRight.right = right;
}