-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode0226.java
More file actions
37 lines (34 loc) · 904 Bytes
/
Copy pathLeetCode0226.java
File metadata and controls
37 lines (34 loc) · 904 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
30
31
32
33
34
35
36
37
/* Invert Binary Tree
* Input:
* 4
/ \
2 7
/ \ / \
1 3 6 9
* Output:
* 4
/ \
7 2
/ \ / \
9 6 3 1
* */
public class LeetCode0226 {
public static int index = 0;
public static int[] TREE_VALUE = new int[]{4, 2, 1, 0, 0, 3, 0, 0, 7, 6, 0, 0, 9, 0, 0};
public static void main(String args[]) {
TreeNode root = new TreeNode();
root = TreeNode.createTree(root, index, TREE_VALUE);
//TreeNode.output(root);
TreeNode t = invertTree(root);
TreeNode.output(t);
}
public static TreeNode invertTree(TreeNode root) {
if (root == null)
return null;
TreeNode R = invertTree(root.right);
TreeNode L = invertTree(root.left);
root.right = L;
root.left = R;
return root;
}
}