-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreeOrder.java
More file actions
159 lines (153 loc) · 4.34 KB
/
BinaryTreeOrder.java
File metadata and controls
159 lines (153 loc) · 4.34 KB
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
import java.util.*;
class Solution{
static class TreeNode{
int val;
TreeNode left;
TreeNode right;
TreeNode(int val){
this.val = val;
this.left = null;
this.right = null;
};
}
public static void main(String[] args) {
TreeNode t1 = new TreeNode(1);
TreeNode t2 = new TreeNode(2);
TreeNode t3 = new TreeNode(3);
TreeNode t4 = new TreeNode(4);
TreeNode t5 = new TreeNode(5);
TreeNode t6 = new TreeNode(6);
TreeNode t7 = new TreeNode(7);
t1.left = t2;
t1.right = t3;
t2.left = t4;
t3.left = t6;
t6.right = t7;
System.out.println("采用递归前序遍历结果:-----");
preOrder1(t1);
System.out.println();
System.out.println("采用迭代前序遍历结果:-----");
preOrder2(t1);
System.out.println();
System.out.println("采用递归中序遍历结果:-----");
inOrder1(t1);
System.out.println();
System.out.println("采用迭代中序遍历结果:-----");
inOrder2(t1);
System.out.println();
System.out.println("采用递归后序遍历结果:-----");
postOrder1(t1);
System.out.println();
System.out.println("采用迭代后序遍历结果:-----");
postOrder2(t1);
System.out.println();
System.out.println("采用迭代后序遍历结果:-----");
postOrder3(t1);
}
/**
采用递归的方法进行前序遍历
*/
public static void preOrder1(TreeNode root){
if(root == null) return;
System.out.print(root.val);
preOrder1(root.left);
preOrder1(root.right);
}
/**
采用递归的方法进行前序遍历
*/
public static void preOrder2(TreeNode root){
if(root == null) return;
Stack<TreeNode> stack = new Stack<TreeNode>();
List<Integer> res = new ArrayList<Integer>();
while(root !=null || (!stack.isEmpty())){
if(root != null){
res.add(root.val);//步骤一,取根节点的值
stack.push(root);//把根节点放入栈中
root=root.left;//步骤二,遍历左子树
}else{
TreeNode tem=stack.pop();
root=tem.right;//步骤三,遍历右子树
}
}
for(int i:res) System.out.print(i);
}
/**
采用递归的方法进行中序遍历
*/
public static void inOrder1(TreeNode root){
if(root == null) return;
inOrder1(root.left);
System.out.print(root.val);
inOrder1(root.right);
}
/**
采用迭代的方法进行中序遍历
*/
public static void inOrder2(TreeNode root){
if(root == null) return;
Stack<TreeNode> stack = new Stack<TreeNode>();
List<Integer> res = new ArrayList<Integer>();
while(root!=null||(!stack.isEmpty())){
if(root!=null){
stack.push(root);//把根节点放入栈中
root=root.left;//步骤一,遍历左子树
}
else{
TreeNode tem=stack.pop();
res.add(tem.val);//步骤二,取根结点的值
root=tem.right;//步骤三,遍历右子树
}
}
for(int i:res) System.out.print(i);
}
/**
采用递归的方法进行后序遍历
*/
public static void postOrder1(TreeNode root){
if(root == null) return;
postOrder1(root.left);
postOrder1(root.right);
System.out.print(root.val);
}
/**
采用迭代的方法进行后序遍历
*/
public static void postOrder2(TreeNode root){
List<Integer> res=new ArrayList<>();
Stack<TreeNode> stack=new Stack<>();
while(root!=null||(!stack.empty())){
if(root!=null){
stack.push(root);//把根节点放入栈中
res.add(0,root.val);//步骤一,在index=0处插入根结点的值
root=root.right;//步骤二,遍历右子树
}
else{
TreeNode tem=stack.pop();
root=tem.left;//步骤三,遍历左子树
}
}
for(int i:res) System.out.print(i);
}
public static void postOrder3(TreeNode root){
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode prev = null;
while(root!=null||(!stack.isEmpty())){
while(root!=null){
stack.push(root);
root = root.left;
}
root = stack.pop();
if (root.right == null || root.right == prev) {
res.add(root.val);
prev = root;
root = null;
} else {
stack.push(root);
root = root.right;
}
}
for(int i:res) System.out.print(i);
}
}