-
Notifications
You must be signed in to change notification settings - Fork 1
/
Question27.java
95 lines (85 loc) · 2.06 KB
/
Question27.java
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.hncboy.swordreferstooffer;
import java.util.LinkedList;
import java.util.Queue;
/**
* @author hncboy
* @date 2020/3/1 21:04
* @description 剑指 Offer 27.二叉树的镜像
* <p>
* 请完成一个函数,输入一个二叉树,该函数输出它的镜像。
* <p>
* 例如输入:
* 4
* / \
* 2 7
* / \ / \
* 1 3 6 9
* 镜像输出:
* <p>
* 4
* / \
* 7 2
* / \ / \
* 9 6 3 1
* <p>
* <p>
* 示例 1:
* 输入:root = [4,2,7,1,3,6,9]
* 输出:[4,7,2,9,6,3,1]
* <p>
* <p>
* 限制:
* 0 <= 节点个数 <= 1000
* 注意:本题与主站 226 题 {@link com.hncboy.InvertBinaryTree}
* 相同:https://leetcode-cn.com/problems/invert-binary-tree/
* <p>
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
public class Question27 {
/**
* BFS
*/
public TreeNode mirrorTree2(TreeNode root) {
if (root == null) {
return null;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
// 从队列中取出节点并交换左右子树
TreeNode node = queue.poll();
TreeNode left = node.left;
node.left = node.right;
node.right = left;
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
return root;
}
/**
* 递归
*/
public TreeNode mirrorTree(TreeNode root) {
if (root == null) {
return null;
}
TreeNode temp = root.left;
root.left = mirrorTree(root.right);
root.right = mirrorTree(temp);
return root;
}
private static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
}