-
Notifications
You must be signed in to change notification settings - Fork 13
/
TreeVisit.java
229 lines (198 loc) · 5.37 KB
/
TreeVisit.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package algorithm.note;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.LinkedList;
/**
* @author: mayuan
* @desc: 二叉树的遍历问题
* @date: 2018/09/03
*/
public class TreeVisit {
public static void main(String[] args) {
/**
* 0
* 1 2
* 3 4 5 6
*/
TreeNode node6 = new TreeNode(null, 6, null);
TreeNode node5 = new TreeNode(null, 5, null);
TreeNode node4 = new TreeNode(null, 4, null);
TreeNode node3 = new TreeNode(null, 3, null);
TreeNode node2 = new TreeNode(node5, 2, node6);
TreeNode node1 = new TreeNode(node3, 1, node4);
TreeNode node0 = new TreeNode(node1, 0, node2);
System.out.println("二叉树前序遍历");
preOrder(node0);
System.out.println();
preOrder2(node0);
System.out.println();
System.out.println("二叉树中序遍历");
inorderTraversal(node0);
System.out.println();
inorderTraversal2(node0);
System.out.println();
System.out.println("二叉树后序遍历");
postTraversal(node0);
System.out.println();
postTraversal2(node0);
System.out.println();
}
/**
* 二叉树 前序遍历(递归)
*
* @param node
*/
public static void preOrder(TreeNode node) {
if (null == node) {
return;
}
visit(node);
preOrder(node.left);
preOrder(node.right);
}
/**
* 二叉树 前序遍历(非递归)
*
* @param node
*/
public static void preOrder2(TreeNode node) {
if (null == node) {
return;
}
LinkedList<TreeNode> stack = new LinkedList<>();
stack.push(node);
while (!stack.isEmpty()) {
TreeNode cur = stack.pop();
visit(cur);
if (null != cur.right) {
stack.push(cur.right);
}
if (null != cur.left) {
stack.push(cur.left);
}
}
}
/**
* 二叉树中序遍历
*
* @param root
*/
public static void inorderTraversal(TreeNode root) {
if (null == root) {
return;
}
inorderTraversal(root.left);
visit(root);
inorderTraversal(root.right);
}
/**
* 二叉树中序遍历(非递归)
*
* @param root
*/
public static void inorderTraversal2(TreeNode root) {
if (null == root) {
return;
}
LinkedList<TreeNode> stack = new LinkedList<>();
TreeNode cur = root;
while (null != cur || !stack.isEmpty()) {
while (null != cur) {
stack.push(cur);
cur = cur.left;
}
cur = stack.pop();
visit(cur);
cur = cur.right;
}
}
/**
* 二叉树后序遍历
*
* @param root
*/
public static void postTraversal(TreeNode root) {
if (null == root) {
return;
}
postTraversal(root.left);
postTraversal(root.right);
visit(root);
}
/**
* 二叉树后序遍历(非递归)
*
* @param root
*/
public static void postTraversal2(TreeNode root) {
if (null == root) {
return;
}
LinkedList<TreeNode> stack = new LinkedList<>();
// cur:当前访问节点
TreeNode cur = root;
// pLastVisit:上次访问节点
TreeNode pLastVisit = null;
// 先移动到最左子树下,过程中压入栈
while (null != cur) {
stack.push(cur);
cur = cur.left;
}
while (!stack.isEmpty()) {
cur = stack.pop();
//访问根节点的前提是:无右子树或右子树已被访问过
if (null == cur.right || pLastVisit == cur.right) {
visit(cur);
pLastVisit = cur;
} else {
// 根节点再次入栈
stack.push(cur);
// 进入右子树,且可肯定右子树一定不为空
cur = cur.right;
while (null != cur) {
stack.push(cur);
cur = cur.left;
}
}
}
}
/**
* 二叉树的层次遍历
*
* @param node
*/
public static void levelTraversal(TreeNode node) {
if (null == node) {
return;
}
Deque<TreeNode> deque = new ArrayDeque<>();
deque.offerLast(node);
while (!deque.isEmpty()) {
TreeNode current = deque.pollFirst();
visit(current);
if (null != current.left) {
deque.offerLast(current.left);
}
if (null != current.right) {
deque.offerLast(current.right);
}
}
}
public static void visit(TreeNode node) {
System.out.print(node.val + " ");
}
private static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(TreeNode l, int v, TreeNode r) {
this.left = l;
this.right = r;
this.val = v;
}
@Override
public String toString() {
return "val: " + val;
}
}
}