二叉树的遍历

引言

  • 本文展示了二叉树的前序遍历、中序遍历、后序遍历;
  • 每种遍历均用递归和非递归的方法实现。
  • 递归的方法明显比非递归的方法容易理解。
  • 非递归的实现方式有很多种,本文给出了用一个栈实现的方法。

程序

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
package test;

import java.util.Stack;

public class Test {

public static class TreeNode {
int val;
TreeNode left;
TreeNode right;

TreeNode(int x) {
val = x;
}
}

public static TreeNode BinaryTree(int[] array) {
return makeBinaryTreeByArray(array, 1);
}

public static TreeNode makeBinaryTreeByArray(int[] array, int index) {
if (index < array.length) {
int value = array[index];
if (value != 0) {
TreeNode t = new TreeNode(value);
array[index] = 0;
t.left = makeBinaryTreeByArray(array, index * 2);
t.right = makeBinaryTreeByArray(array, index * 2 + 1);
return t;
}
}
return null;
}

//前序遍历 递归实现
public static void preTraversal(TreeNode root){
if(root==null){
return;
}

System.out.print(root.val+" ");
preTraversal(root.left);
preTraversal(root.right);
}

//前序遍历 非递归实现(栈)
public static void preTraversal1(TreeNode root){
Stack<TreeNode> stack = new Stack<>();
if(root==null){
return;
}

stack.push(root);
while(!stack.isEmpty()){
TreeNode tmp = stack.pop();
System.out.print(tmp.val+" ");
if(tmp.right!=null){
stack.push(tmp.right);
}
if(tmp.left!=null){
stack.push(tmp.left);
}
}
}

//中序遍历 递归实现
public static void midTraversal(TreeNode root){
if(root==null){
return;
}

if(root.left!=null){
midTraversal(root.left);
}

System.out.print(root.val+" ");

if(root.right!=null){
midTraversal(root.right);
}
}

//中序遍历 非递归实现(栈)
public static void midTraversal1(TreeNode root){
Stack<TreeNode> stack = new Stack<>();
TreeNode node = root;
while (node != null || stack.size() > 0) {
while (node != null) {
stack.push(node);
node = node.left;
}
if (stack.size() > 0) {
node = stack.pop();
System.out.print(node.val+" ");
node = node.right;
}
}
}

//后序遍历 递归实现
public static void behTraversal(TreeNode root){
if(root==null){
return;
}

if(root.left!=null){
behTraversal(root.left);
}

if(root.right!=null){
behTraversal(root.right);
}

System.out.print(root.val+" ");
}

//后序遍历 非递归实现(栈)
public static void behTraversal1(TreeNode root){
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode node = root, prev = root;
while (node != null || stack.size() > 0) {
while (node != null) {
stack.push(node);
node = node.left;
}
if (stack.size() > 0) {
TreeNode temp = stack.peek().right;
if (temp == null || temp == prev) {
node = stack.pop();
System.out.print(node.val+" ");
prev = node;
node = null;
} else {
node = temp;
}
}
}
}

/**
* 13
* / \
* 65 5
* / \ \
* 97 25 37
* / /\ /
* 22 4 28 32
*/

public static void main(String[] args) {
int[] arr = { 0, 13, 65, 5, 97, 25, 0, 37, 22, 0, 4, 28, 0, 0, 32, 0 };
TreeNode root = BinaryTree(arr);
preTraversal(root); //13 65 97 22 25 4 28 5 37 32
System.out.print("\n");
preTraversal1(root); //13 65 97 22 25 4 28 5 37 32
System.out.print("\n");
midTraversal(root); //22 97 65 4 25 28 13 5 32 37
System.out.print("\n");
midTraversal1(root); //22 97 65 4 25 28 13 5 32 37
System.out.print("\n");
behTraversal(root); //22 97 4 28 25 65 32 37 5 13
System.out.print("\n");
behTraversal1(root); //22 97 4 28 25 65 32 37 5 13
}
}
您的支持是对我最大的鼓励!