forked from TellH/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMirrorTree.java
51 lines (44 loc) · 1.2 KB
/
MirrorTree.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
package SwordToOffer;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
/**
* Created by tlh on 2017/3/13.
*/
public class MirrorTree {
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
public void Mirror(TreeNode root) {
doMirror(root);
}
private TreeNode doMirror(TreeNode root) {
if (root == null) return null;
TreeNode left = root.left;
TreeNode right = root.right;
root.left = doMirror(right);
root.right = doMirror(left);
return root;
}
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
ArrayList<Integer> result = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
if (root == null)
return result;
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
if (node == null)
continue;
result.add(node.val);
queue.add(node.left);
queue.add(node.right);
}
return result;
}
}