数据结构-树

学习数据结构算法题

基本树遍历

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
public class TreeNodeDemo {
static class TreeNode {
int val;
TreeNode left;
TreeNode right;

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

public static void main(String[] args) {
TreeNode node1 = new TreeNode(1);
TreeNode node2 = new TreeNode(2);
TreeNode node3 = new TreeNode(3);
TreeNode node4 = new TreeNode(4);
TreeNode node5 = new TreeNode(5);
node1.left = node2;
node1.right = node3;
node2.left = node4;
node2.right = node5;

List<List<Integer>> ret = new ArrayList<>();

afterOrderTreeStack(node1);
}

// 先序遍历
private static void beforeOrderTree(TreeNode node) {
if (node == null) return;
System.out.println(node.val);
beforeOrderTree(node.left);
beforeOrderTree(node.right);
}

private static void beforeOrderTreeStack(TreeNode node) {
if (node == null) return;
Stack<TreeNode> stack = new Stack<>();
stack.push(node);
while (!stack.isEmpty()) {
TreeNode pop = stack.pop();
System.out.println(pop.val);
if (pop.right != null) stack.push(pop.right);
if (pop.left != null) stack.push(pop.left);
}
}

// 中序遍历
private static void mobileOrderTree(TreeNode node) {
if (node == null) return;
beforeOrderTree(node.left);
System.out.println(node.val);
beforeOrderTree(node.right);
}

private static void mobileOrderTreeStack(TreeNode node) {
if (node == null) return;
Stack<TreeNode> stack = new Stack<>();
while (node != null || !stack.isEmpty()) {
while (node != null) {
stack.push(node);
node = node.left;
}
TreeNode pop = stack.pop();
System.out.println(pop.val);
node = pop.right;
}
}

// 后序遍历
private static void afterOrderTree(TreeNode node) {
if (node == null) return;
beforeOrderTree(node.left);
System.out.println(node.val);
beforeOrderTree(node.right);
}

private static void afterOrderTreeStack(TreeNode node) {
if (node == null) return;
Stack<TreeNode> stack = new Stack<>();
stack.push(node);
while (!stack.isEmpty()) {
TreeNode pop = stack.pop();
System.out.println(pop.val);
if (pop.left != null) stack.push(pop.left);
if (pop.right != null) stack.push(pop.right);
}
// 输出需要进行反转
}

// 层级遍历-递归
private static void levelOrderTreeRecursion(List<List<Integer>> ret, TreeNode node, int deep) {
if (node == null) return;
if (ret.size() == deep) ret.add(new ArrayList<>());
ret.get(deep).add(node.val);
levelOrderTreeRecursion(ret, node.left, deep + 1);
levelOrderTreeRecursion(ret, node.right, deep + 1);
}

// 层级遍历-队列
private static void levelOrderTreeQueue(TreeNode node) {
if (node == null) {
return;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.add(node);
while (!queue.isEmpty()) {
TreeNode poll = queue.poll();
System.out.println(poll.val);
if (poll.left != null) queue.add(poll.left);
if (poll.right != null) queue.add(poll.right);
}
}
}