-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo19.java
93 lines (82 loc) · 1.95 KB
/
No19.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
package week_4;
/**难度系数:***
* 剑指offer: 树的镜像
* 方法:前序遍历,交换左右子树
* 测试用例:功能测试(正常树/树为空/单节点/只有左子树或右子树)
* @author dingding
* Date:2017-7-3 10:35
* Declaration: All Rights Reserved!
*/
public class No19 {
public static void main(String[] args) {
test1();
test2();
test3();
test4();
}
//solution,前序遍历
private static void MirrorRecursively(BinaryTreeNode root){
//递归结束条件
if (root == null) {
return;
}
//到叶子节点
if (root.left == null && root.right == null) {
return;
}
//交换左右子树
BinaryTreeNode pTemp = root.left;
root.left = root.right;
root.right = pTemp;
if (root.left!=null) {
MirrorRecursively(root.left);
}
if (root.right!=null) {
MirrorRecursively(root.right);
}
}
//中序遍历打印二叉树
private static void printTree(BinaryTreeNode root){
if (root!=null){
printTree(root.left);
System.out.print(root.value + " ");
printTree(root.right);
}
}
/*==================测试用例================*/
//普通二叉树
private static void test1() {
BinaryTreeNode root = new BinaryTreeNode(8);
root.left = new BinaryTreeNode(6);
root.right = new BinaryTreeNode(10);
root.left.left = new BinaryTreeNode(5);
root.left.right = new BinaryTreeNode(7);
root.right.left = new BinaryTreeNode(9);
root.right.right = new BinaryTreeNode(11);
MirrorRecursively(root);
printTree(root);
System.out.println();
}
//只有左子树
private static void test2() {
BinaryTreeNode root = new BinaryTreeNode(8);
root.left = new BinaryTreeNode(6);
root.left.left = new BinaryTreeNode(5);
MirrorRecursively(root);
printTree(root);
System.out.println();
}
//只有一个节点
private static void test3() {
BinaryTreeNode root = new BinaryTreeNode(8);
root.left = new BinaryTreeNode(6);
MirrorRecursively(root);
printTree(root);
System.out.println();
}
//空树
private static void test4() {
MirrorRecursively(null);
printTree(null);
}
}