forked from shijiebei2009/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDepthFirstTraversal.java
67 lines (61 loc) · 1.56 KB
/
DepthFirstTraversal.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
package cn.codepub.algorithms.trees;
import cn.codepub.algorithms.utils.Tree;
import java.util.Stack;
/**
* <p>
* Created with IntelliJ IDEA. 2015/10/30 17:32
* </p>
* <p>
* ClassName:DepthFirstTraversal
* </p>
* <p>
* Description:二叉树的深度优先遍历
* </P>
*
* @author Wang Xu
* @version V1.0.0
* @since V1.0.0
*/
public class DepthFirstTraversal {
static Stack<Tree> stack = new Stack();
public static void main(String[] args) {
Tree root = new Tree(1);
Tree l1 = new Tree(3);
Tree l2 = new Tree(9);
Tree l3 = new Tree(11);
Tree r1 = new Tree(5);
Tree r2 = new Tree(12);
Tree r3 = new Tree(13);
root.left = l1;
root.right = r1;
l1.left = l2;
l2.left = l3;
l3.right = r2;
l3.left = new Tree(14);
r2.right = r3;
Tree r4 = new Tree(88);
r1.right = r4;
dfs(root);
}
public static void dfs(Tree root) {
if (null == root) {
return;
}
//设置访问标记
root.isVisited = true;
//入栈
stack.push(root);
//访问结点
//开始弹栈
if (!stack.isEmpty()) {
Tree pop = stack.pop();
System.out.print(pop.value + "\t");
if (pop.left != null && !pop.left.isVisited) {
dfs(pop.left);
}
if (pop.right != null && !pop.right.isVisited) {
dfs(pop.right);
}
}
}
}