-
Notifications
You must be signed in to change notification settings - Fork 43
/
InvertBinaryTree.java
130 lines (112 loc) · 3.58 KB
/
InvertBinaryTree.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package LeetCodeJava.Tree;
// https://leetcode.com/problems/invert-binary-tree/
import LeetCodeJava.DataStructure.TreeNode;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class InvertBinaryTree {
// VO
// IDEA : DFS + cache
public TreeNode invertTree(TreeNode root) {
if (root == null){
return root;
}
// NOTE !!! we cache left sub tree first
// then can assign such node to right sub tree
TreeNode tmp = root.left;
root.left = invertTree(root.right);
root.right = invertTree(tmp);
return root;
}
// V0'
// IDEA : DFS
public TreeNode invertTree_0(TreeNode root) {
if (root == null) {
return null;
}
/** NOTE !!!!
*
* instead of calling invertTree and assign value to sub tree directly,
* we need to CACHE invertTree result, and assign later
* -> since assign directly will cause tree changed, and affect the other invertTree call
*
* e.g. below is WRONG,
* root.left = invertTree(root.right);
* root.right = invertTree(root.left);
*
* need to cache result
*
* TreeNode left = invertTree(root.left);
* TreeNode right = invertTree(root.right);
*
* then assign to sub tree
*
* root.left = right;
* root.right = left;
*/
TreeNode left = invertTree_0(root.left);
TreeNode right = invertTree_0(root.right);
root.left = right;
root.right = left;
/** NOTE !!!! below is WRONG */
// root.left = invertTree(root.right);
// root.right = invertTree(root.left);
return root;
}
// V0
// IDEA : BFS
public TreeNode invertTree_(TreeNode root) {
if (root == null) {
return null;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.empty()) {
for (int i = 0; i < stack.size(); i ++){
TreeNode cur = stack.pop();
// NOTE : trick here
TreeNode tmp1 = cur.left;
TreeNode tmp2 = cur.right;
cur.left = tmp2;
cur.right = tmp1;
if (cur.left != null) {
stack.push(cur.left);
}
if (cur.right != null) {
stack.push(cur.right);
}
}
}
return root;
}
// V1
// IDEA: Recursive
// https://leetcode.com/problems/invert-binary-tree/editorial/
public TreeNode invertTree_2(TreeNode root) {
if (root == null) {
return null;
}
TreeNode right = invertTree_2(root.right);
TreeNode left = invertTree_2(root.left);
root.left = right;
root.right = left;
return root;
}
// V1'
// IDEA : Iterative
// https://leetcode.com/problems/invert-binary-tree/editorial/
public TreeNode invertTree_3(TreeNode root) {
if (root == null) return null;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode current = queue.poll();
TreeNode temp = current.left;
current.left = current.right;
current.right = temp;
if (current.left != null) queue.add(current.left);
if (current.right != null) queue.add(current.right);
}
return root;
}
}