算法: 二叉树构造遍历等

废话不多说, 上代码:

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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package com.hongyan.learn.test.dataStructures;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

/**
* @author weihongyan
* @description binary tree
* @date 10/03/2017
*/
@Slf4j
public class BuildTreeFromTraversalAndConvertTreeToTraversal {
@Test
public void test() {
int[] preOrder = { 1, 2, 4, 5, 6, 7, 3, 8, 9 };
int[] inOrder = { 5, 4, 7, 6, 2, 1, 8, 3, 9 };
int[] postOrder = { 5, 7, 6, 4, 2, 8, 9, 3, 1 };
TreeNode root = this.preOrderInOrderBuild(preOrder, 0, inOrder, 0, inOrder.length - 1);
TreeNode root2 = this.postOrderInOrderBuild(postOrder, postOrder.length - 1, inOrder, 0, inOrder.length - 1);
List<Integer> preResult = new ArrayList<>();
this.preTraversalRecur(root, preResult);
System.out.println(preResult);
System.out.println(this.preTraversalIter(root2));
List<Integer> inResult = new ArrayList<>();
this.inTraversalRecur(root, inResult);
System.out.println(inResult);
System.out.println(this.inTraversalIter(root2));
List<Integer> postResult = new ArrayList<>();
this.postTraversalRecur(root, postResult);
System.out.println(postResult);
System.out.println(this.postTraversalIter(root2));
System.out.println(this.levelTraversal(root));
System.out.println(this.levelTraversal(root2));
}

/**
* preOrder recursion traversal
*
* @param node
* @param result
*/
public void preTraversalRecur(TreeNode node, List<Integer> result) {
if (null == node) {
return;
}
result.add(node.val);
preTraversalRecur(node.left, result);
preTraversalRecur(node.right, result);
}

/**
* preOrder traversal iteration
*
* @param root
* @return
*/
public List<Integer> preTraversalIter(TreeNode root) {
List<Integer> result = new ArrayList<>();
Deque<TreeNode> stack = new LinkedList<>();
TreeNode temp = root;
while (temp != null || stack.size() > 0) {
while (temp != null) {
stack.push(temp);
result.add(temp.val);
temp = temp.left;
}
temp = stack.pop().right;
}
return result;
}

/**
* inOrder recursion traversal
*
* @param node
* @param result
*/
public void inTraversalRecur(TreeNode node, List<Integer> result) {
if (null == node) {
return;
}
inTraversalRecur(node.left, result);
result.add(node.val);
inTraversalRecur(node.right, result);
}

/**
* inOrder traversal Iteration
*
* @param root
* @return
*/
public List<Integer> inTraversalIter(TreeNode root) {
List<Integer> result = new ArrayList<>();
Deque<TreeNode> stack = new LinkedList<>();
TreeNode temp = root;
while (temp != null || stack.size() > 0) {
while (temp != null) {
stack.push(temp);
temp = temp.left;
}
temp = stack.pop();
result.add(temp.val);
temp = temp.right;
}
return result;
}

/**
* postOrder recursion traversal
*
* @param node
* @param result
*/
public void postTraversalRecur(TreeNode node, List<Integer> result) {
if (null == node) {
return;
}
postTraversalRecur(node.left, result);
postTraversalRecur(node.right, result);
result.add(node.val);
}

/**
* postOrder iteration traversal
*
* @param root
* @return
*/
public List<Integer> postTraversalIter(TreeNode root) {
LinkedList<Integer> resultQueue = new LinkedList<>();
Deque<TreeNode> stack = new LinkedList<>();
TreeNode temp = root;
while (temp != null || stack.size() > 0) {
while (temp != null) {
stack.push(temp);
resultQueue.addFirst(temp.val);
temp = temp.right;
}
temp = stack.pop().left;
}
return resultQueue;
}

/**
* build a binary tree from preOrder and inOrder
*
* @param preOrder
* @param preStart
* @param inOrder
* @param inStart
* @param inEnd
* @return
*/
public TreeNode preOrderInOrderBuild(int[] preOrder, int preStart, int[] inOrder, int inStart, int inEnd) {
if (preStart >= preOrder.length || inStart > inEnd) {
return null;
}
TreeNode root = new TreeNode(preOrder[preStart]);
int pos = -1;
for (int i = inStart; i <= inEnd; i++) {
if (inOrder[i] == preOrder[preStart]) {
pos = i;
break;
}
}
root.left = preOrderInOrderBuild(preOrder, preStart + 1, inOrder, inStart, pos - 1);
root.right = preOrderInOrderBuild(preOrder, preStart + (pos - inStart) + 1, inOrder, pos + 1, inEnd);
return root;
}

/**
* build a binary tree from postOrder and inOrder
*
* @param postOrder
* @param postEnd
* @param inOrder
* @param inStart
* @param inEnd
* @return
*/
public TreeNode postOrderInOrderBuild(int[] postOrder, int postEnd, int[] inOrder, int inStart, int inEnd) {
if (postEnd < 0 || inStart > inEnd) {
return null;
}
TreeNode root = new TreeNode(postOrder[postEnd]);
int pos = -1;
for (int i = inStart; i <= inEnd; i++) {
if (postOrder[postEnd] == inOrder[i]) {
pos = i;
break;
}
}
root.left = this.postOrderInOrderBuild(postOrder, postEnd - (inEnd - pos) - 1, inOrder, inStart, pos - 1);
root.right = this.postOrderInOrderBuild(postOrder, postEnd - 1, inOrder, pos + 1, inEnd);
return root;
}

/**
* fucking failed! can not build a tree from preOrder and postOrder
*
* @param preOrder
* @param preStart
* @param preEnd
* @param postOrder
* @param postStart
* @param postEnd
* @return
*/
public TreeNode preOrderPostOrderBuild(int[] preOrder, int preStart, int preEnd, int[] postOrder, int postStart,
int postEnd) {
if (preStart > preEnd || postStart > postEnd) {
return null;
}
TreeNode root = new TreeNode(preOrder[preStart]);
return null;
}

/**
* level traversal
*
* @param root
* @return
*/
public List<Integer> levelTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
Deque<TreeNode> queue = new LinkedList<>();
queue.offer(root);
TreeNode temp;
while (queue.size() > 0) {
for (int i = queue.size(); i > 0; i--) {
temp = queue.poll();
result.add(temp.val);
if (null != temp.left) {
queue.offer(temp.left);
}
if (null != temp.right) {
queue.offer(temp.right);
}
}
}
return result;
}

private static class TreeNode {
int val;
TreeNode left;
TreeNode right;

TreeNode(int val) {
this.val = val;
}
}
}