-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathDepthPrint.java
83 lines (74 loc) · 2.07 KB
/
DepthPrint.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
package ds;
import java.util.HashSet;
import java.util.Set;
import java.util.Stack;
/**
* 还是以一棵树为例,深度优先遍历一遍看结果
*
* @author yangyi 2019年01月27日22:28:39
*/
public class DepthPrint {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
}
public void depthPrint(TreeNode node) {
if (node == null) {
return;
}
Stack<TreeNode> stack = new Stack<>();
Set<TreeNode> set = new HashSet<>();
TreeNode current;
stack.push(node);
set.add(node);
while (!stack.isEmpty()) {
current = stack.pop();
System.out.println(current.val);
if (current.right != null) {
if (!set.contains(current.right)) {
stack.push(current.right);
set.add(current.right);
}
}
if (current.left != null) {
if (!set.contains(current.left)) {
stack.push(current.left);
set.add(current.left);
}
}
}
}
public TreeNode createTree() {
TreeNode one = new TreeNode();
one.val = 1;
TreeNode two = new TreeNode();
two.val = 2;
TreeNode three = new TreeNode();
three.val = 3;
TreeNode four = new TreeNode();
four.val = 4;
TreeNode five = new TreeNode();
five.val = 5;
TreeNode six = new TreeNode();
six.val = 6;
TreeNode seven = new TreeNode();
seven.val = 7;
TreeNode eight = new TreeNode();
eight.val = 8;
one.left = two;
one.right = five;
two.left = three;
three.left = four;
one.right = five;
five.left = six;
six.left = seven;
five.right = eight;
return one;
}
public static void main(String[] args) {
DepthPrint depthPrint = new DepthPrint();
TreeNode node = depthPrint.createTree();
depthPrint.depthPrint(node);
}
}