forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT18.java
More file actions
29 lines (26 loc) · 723 Bytes
/
T18.java
File metadata and controls
29 lines (26 loc) · 723 Bytes
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
package web; /**
* @program LeetNiu
* @description: 二叉树的镜像
* @author: mf
* @create: 2020/01/10 16:18
*/
/**
* 操作给定的二叉树,将其变换为源二叉树的镜像。
*/
public class T18 {
public void Mirror(TreeNode root) {
// 判断
if (root == null) return;
if (root.left == null && root.right == null) return;
// 交换
swap(root, root.left, root.right);
// 递归
if (root.left != null) Mirror(root.left);
if (root.right != null) Mirror(root.right);
}
private void swap(TreeNode root, TreeNode left, TreeNode right) {
TreeNode temp = left;
root.left = right;
root.right = temp;
}
}