Skip to content

Commit 0fa1eae

Browse files
committed
二叉树的深度优先遍历和广度优先遍历
1 parent cdf1ad6 commit 0fa1eae

File tree

2 files changed

+321
-0
lines changed

2 files changed

+321
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,9 @@
7373
[detect-ring]: ./src/com/fantasy/algorithm/linkedlist/DetectRing.java
7474
[remove-node-from-end]: ./src/com/fantasy/algorithm/linkedlist/RemoveNodeFromEnd.java
7575
[palindrome-linked-list]: ./src/com/fantasy/algorithm/linkedlist/PalindromeLinkedList.java
76+
77+
### 二叉树
78+
79+
- [二叉树的深度优先遍历和广度优先遍历][traversal-binary-search-tree]
80+
81+
[traversal-binary-search-tree]: ./src/com/fantasy/algorithm/tree/TraversalBinarySearchTree.java
Lines changed: 315 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,315 @@
1+
package com.fantasy.algorithm.tree;
2+
3+
import java.util.ArrayList;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.Queue;
7+
import java.util.Stack;
8+
9+
import com.fantasy.datastructure.tree.BinarySearchTree;
10+
import com.fantasy.datastructure.tree.TreeNode;
11+
12+
/**
13+
* 遍历二叉查找树
14+
*
15+
* <pre>
16+
* author : Fantasy
17+
* version : 1.0, 2020-08-17
18+
* since : 1.0, 2020-08-17
19+
* </pre>
20+
*/
21+
public class TraversalBinarySearchTree {
22+
23+
public static void main(String[] args) {
24+
// 3
25+
// / \
26+
// 2 5
27+
// / / \
28+
// 1 4 6
29+
BinarySearchTree tree = new BinarySearchTree();
30+
tree.insert(3);
31+
tree.insert(2);
32+
tree.insert(5);
33+
tree.insert(1);
34+
tree.insert(4);
35+
tree.insert(6);
36+
37+
// 先序遍历
38+
System.out.print("preorder : ");
39+
preorder(tree.getRoot()); // 3 2 1 5 4 6
40+
System.out.println();
41+
System.out.println("preorder2 : " + preorder2(tree.getRoot()));
42+
System.out.println("preorder3 : " + preorder3(tree.getRoot()));
43+
44+
// 中序遍历
45+
System.out.print("inorder : ");
46+
inorder(tree.getRoot()); // 1 2 3 4 5 6
47+
System.out.println();
48+
System.out.println("inorder2 : " + inorder2(tree.getRoot()));
49+
50+
// 后序遍历
51+
System.out.print("postorder : ");
52+
postorder(tree.getRoot()); // 1 2 4 6 5 3
53+
System.out.println();
54+
System.out.println("postorder2 : " + postorder2(tree.getRoot()));
55+
System.out.println("postorder3 : " + postorder3(tree.getRoot()));
56+
57+
// 广度优先遍历
58+
System.out.println("level order : " + levelOrder(tree.getRoot())); // 3 2 5 1 4 6
59+
System.out.println("level order2 : " + levelOrder2(tree.getRoot()));
60+
}
61+
62+
/**
63+
* 先序遍历(递归)
64+
*
65+
* @param root 根结点
66+
*/
67+
public static void preorder(TreeNode root) {
68+
if (root == null) {
69+
return;
70+
}
71+
72+
System.out.print(root.value + " ");
73+
preorder(root.left);
74+
preorder(root.right);
75+
}
76+
77+
/**
78+
* 中序遍历(递归)
79+
*
80+
* @param root 根结点
81+
*/
82+
public static void inorder(TreeNode root) {
83+
if (root == null) {
84+
return;
85+
}
86+
87+
inorder(root.left);
88+
System.out.print(root.value + " ");
89+
inorder(root.right);
90+
}
91+
92+
/**
93+
* 后序遍历(递归)
94+
*
95+
* @param root 根结点
96+
*/
97+
public static void postorder(TreeNode root) {
98+
if (root == null) {
99+
return;
100+
}
101+
102+
postorder(root.left);
103+
postorder(root.right);
104+
System.out.print(root.value + " ");
105+
}
106+
107+
/**
108+
* 先序遍历(非递归)
109+
*
110+
* @param root 根结点
111+
* @return 结果
112+
*/
113+
public static List<Integer> preorder2(TreeNode root) {
114+
List<Integer> list = new ArrayList<>();
115+
if (root == null) {
116+
return list;
117+
}
118+
119+
TreeNode temp = null;
120+
Stack<TreeNode> stack = new Stack<>();
121+
stack.push(root);
122+
123+
while (!stack.empty()) {
124+
temp = stack.pop();
125+
list.add(temp.value);
126+
if (temp.right != null) {
127+
stack.push(temp.right);
128+
}
129+
if (temp.left != null) {
130+
stack.push(temp.left);
131+
}
132+
}
133+
134+
return list;
135+
}
136+
137+
/**
138+
* 先序遍历(非递归)
139+
*
140+
* @param root 根结点
141+
* @return 结果
142+
*/
143+
public static List<Integer> preorder3(TreeNode root) {
144+
List<Integer> list = new ArrayList<>();
145+
Stack<TreeNode> stack = new Stack<>();
146+
TreeNode temp = root;
147+
148+
while (temp != null || !stack.empty()) {
149+
while (temp != null) {
150+
list.add(temp.value);
151+
stack.push(temp);
152+
temp = temp.left;
153+
}
154+
temp = stack.pop();
155+
temp = temp.right;
156+
}
157+
158+
return list;
159+
}
160+
161+
/**
162+
* 中序遍历(非递归)
163+
*
164+
* @param root 根结点
165+
* @return 结果
166+
*/
167+
public static List<Integer> inorder2(TreeNode root) {
168+
List<Integer> list = new ArrayList<>();
169+
Stack<TreeNode> stack = new Stack<>();
170+
TreeNode temp = root;
171+
172+
while (temp != null || !stack.empty()) {
173+
while (temp != null) {
174+
stack.push(temp);
175+
temp = temp.left;
176+
}
177+
temp = stack.pop();
178+
list.add(temp.value);
179+
temp = temp.right;
180+
}
181+
182+
return list;
183+
}
184+
185+
/**
186+
* 后序遍历(非递归)
187+
*
188+
* @param root 根结点
189+
* @return 结果
190+
*/
191+
public static List<Integer> postorder2(TreeNode root) {
192+
List<Integer> list = new ArrayList<>();
193+
if (root == null) {
194+
return list;
195+
}
196+
197+
TreeNode temp = null;
198+
Stack<TreeNode> stack = new Stack<>();
199+
stack.push(root);
200+
201+
while (!stack.empty()) {
202+
temp = stack.pop();
203+
if (temp.left != null) {
204+
stack.push(temp.left);
205+
}
206+
if (temp.right != null) {
207+
stack.push(temp.right);
208+
}
209+
list.add(0, temp.value); // 逆序添加节点值
210+
}
211+
212+
return list;
213+
}
214+
215+
/**
216+
* 后序遍历(非递归)
217+
*
218+
* @param root 根结点
219+
* @return 结果
220+
*/
221+
public static List<Integer> postorder3(TreeNode root) {
222+
List<Integer> list = new ArrayList<>();
223+
Stack<TreeNode> stack = new Stack<>();
224+
TreeNode cur = root;
225+
TreeNode last = null; // 记录上一次访问的节点
226+
227+
while (cur != null || !stack.empty()) {
228+
// 从根节点开始,将其所有左子节点入栈
229+
while (cur != null) {
230+
stack.push(cur);
231+
cur = cur.left;
232+
}
233+
cur = stack.peek();
234+
if (cur.right == null || cur.right == last) {
235+
// 如果当前节点没有右孩子,或者当前节点的右孩子刚刚被访问过,
236+
// 这时应该访问当前节点
237+
list.add(cur.value);
238+
stack.pop();
239+
last = cur;
240+
cur = null;
241+
} else {
242+
// 否则,将当前指针移到其右孩子节点上
243+
cur = cur.right;
244+
}
245+
}
246+
247+
return list;
248+
}
249+
250+
/**
251+
* 广度优先遍历二叉树,又称层次遍历二叉树
252+
*
253+
* @param root 根结点
254+
* @return 结果
255+
*/
256+
public static List<Integer> levelOrder(TreeNode root) {
257+
List<Integer> list = new ArrayList<>();
258+
if (root == null) {
259+
return list;
260+
}
261+
262+
TreeNode temp = null;
263+
Queue<TreeNode> queue = new LinkedList<>();
264+
queue.offer(root);
265+
266+
while (!queue.isEmpty()) {
267+
temp = queue.poll();
268+
list.add(temp.value);
269+
if (temp.left != null) {
270+
queue.offer(temp.left);
271+
}
272+
if (temp.right != null) {
273+
queue.offer(temp.right);
274+
}
275+
}
276+
277+
return list;
278+
}
279+
280+
/**
281+
* 广度优先遍历二叉树,又称层次遍历二叉树
282+
*
283+
* @param root 根结点
284+
* @return 结果
285+
*/
286+
public static List<List<Integer>> levelOrder2(TreeNode root) {
287+
List<List<Integer>> list = new ArrayList<>();
288+
if (root == null) {
289+
return list;
290+
}
291+
292+
List<Integer> level = null;
293+
Queue<TreeNode> queue = new LinkedList<>();
294+
queue.offer(root);
295+
296+
while (!queue.isEmpty()) {
297+
level = new ArrayList<>(); // 一层一个list
298+
int size = queue.size();
299+
for (int i = 0; i < size; i++) {
300+
TreeNode node = queue.poll();
301+
level.add(node.value);
302+
if (node.left != null) {
303+
queue.offer(node.left);
304+
}
305+
if (node.right != null) {
306+
queue.offer(node.right);
307+
}
308+
}
309+
list.add(level);
310+
}
311+
312+
return list;
313+
}
314+
315+
}

0 commit comments

Comments
 (0)