【algorithm】二叉树的遍历

二叉树的遍历

二叉树用例

代码解析:

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
public class BinaryTree {

static class TreeNode {
Integer val;
TreeNode left;
TreeNode right;

public TreeNode(Integer val) {
this.val = val;
}
}

public static TreeNode init(Integer[] arr, int index) {
TreeNode node = null;
if (index < arr.length) {
node = new TreeNode(arr[index]);
node.left = init(arr, 2 * index + 1);
node.right = init(arr, 2 * index + 2);
}
return node;
}

private static List<Integer> list = new ArrayList<>(10);

public static void main(String[] args) {
Integer[] arr = new Integer[]{1, 3, 4, 5, 6, 7, 8};

System.out.println("递归实现前序遍历: "+ rootLeftRightRecursive(init(arr,0)));
list.clear();
System.out.println("非递归实现前序遍历: "+ rootLeftRightNonRecursive(init(arr,0)));
list.clear();

System.out.println();

System.out.println("递归实现中序遍历: "+ leftRootRightRecursive(init(arr,0)));
list.clear();
System.out.println("非递归实现中序遍历: "+ leftRootRightNonRecursive(init(arr,0)));
list.clear();

System.out.println();

System.out.println("递归实现后序遍历: "+ leftRightRootRecursive(init(arr,0)));
list.clear();
System.out.println("非递归实现后序遍历: "+ leftRightRootNonRecursive(init(arr,0)));
list.clear();

System.out.println();

System.out.println("层次遍历: "+ levelOrder(init(arr,0)));

System.out.println();

System.out.println("树的深度为: "+ depth(init(arr,0)));


}


/**
* 递归实现前序遍历
* 中-左-右
* @param node TreeNode
* @return List
*/
public static List rootLeftRightRecursive(TreeNode node) {
if (null != node){
list.add(node.val);
rootLeftRightRecursive(node.left);
rootLeftRightRecursive(node.right);
}
return list;
}

/**
* 非递归实现前序遍历
* 中-左-右
* @param node TreeNode
* @return List
*/
public static List rootLeftRightNonRecursive(TreeNode node) {
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = node;

while (null != cur || !stack.isEmpty()) {
if (null != cur) {
list.add(cur.val);
stack.push(cur);
cur = cur.left;

} else {
cur = stack.pop();
cur = cur.right;
}
}
return list;
}

/**
* 递归实现中序遍历
* 左-中-右
* @param node TreeNode
* @return List
*/
public static List leftRootRightRecursive(TreeNode node) {
if (null!=node){
leftRootRightRecursive(node.left);
list.add(node.val);
leftRootRightRecursive(node.right);
}
return list;
}

/**
* 非递归实现中序遍历
* 左-中-右
* @param node TreeNode
* @return List
*/
public static List leftRootRightNonRecursive(TreeNode node) {
List<Integer> list = new ArrayList<>(10);
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = node;

while (null != cur || !stack.isEmpty()) {
if (null != cur) {
stack.push(cur);
cur = cur.left;
} else {
cur = stack.pop();
list.add(cur.val);
cur = cur.right;
}
}

return list;
}

/**
* 递归实现后序遍历
* 左-右-中
* @param node TreeNode
* @return List
*/
public static List leftRightRootRecursive(TreeNode node){

if (null!=node){
leftRightRootRecursive(node.left);
leftRightRootRecursive(node.right);
list.add(node.val);
}
return list;
}

/**
* 非递归实现后序遍历
* 左-右-中
* @param node TreeNode
* @return List
*/
public static List leftRightRootNonRecursive(TreeNode node){
if (null == node){
return list;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(node);
TreeNode cur;

while (!stack.isEmpty()){
cur = stack.pop();
if (cur.left!=null){
stack.push(cur.left);
}
if (cur.right!=null){
stack.push(cur.right);
}
// 逆序添加
list.add(0,cur.val);
}
return list;
}

/**
* 层序遍历队列实现(广度优先算法BFS)
* @param root TreeNode
* @return List
*/
public static List<List<Integer>> levelOrder(TreeNode root){
List<List<Integer>> list = new ArrayList<>();
if(root == null){
return list;
}

Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);

while(!queue.isEmpty()){
int count = queue.size();
List<Integer> tmpList = new ArrayList<>();
while(count > 0){
TreeNode node = queue.poll();
tmpList.add(node.val);
if(node.left!=null){
queue.add(node.left);
}
if(node.right!=null){
queue.add(node.right);
}
count--;
}
list.add(tmpList);
}
return list;
}


/**
* 递归实现获取树的深度
* @param node TreeNode
* @return int
*/
public static int depth(TreeNode node){
if (node == null){
return 0;
}
int left = depth(node.left);
int right = depth(node.right);

return left > right ? left + 1 : right + 1;
}

}

结果为:

1
2
3
4
5
6
7
8
9
10
11
12
递归实现前序遍历:   [1, 3, 5, 6, 4, 7, 8]
非递归实现前序遍历: [1, 3, 5, 6, 4, 7, 8]

递归实现中序遍历: [5, 3, 6, 1, 7, 4, 8]
非递归实现中序遍历: [5, 3, 6, 1, 7, 4, 8]

递归实现后序遍历: [5, 6, 3, 7, 8, 4, 1]
非递归实现后序遍历: [5, 6, 3, 7, 8, 4, 1]

层次遍历: [[1], [3, 4], [5, 6, 7, 8]]

树的深度为: 3