-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinaryTree.java
More file actions
94 lines (83 loc) · 2.34 KB
/
Copy pathBinaryTree.java
File metadata and controls
94 lines (83 loc) · 2.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
package tree;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
public class BinaryTree {
public static List<Integer> preOrderTraversalWithoutRecursion(TreeNode root){
List<Integer> ans = new LinkedList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode p = root;
while(p != null || !stack.isEmpty()){
if(p != null){
ans.add(p.val); //前序遍历入栈就加入到ans中
stack.push(p);
p = p.left;
}else{
p = stack.pop();
p = p.right;
}
}
return ans;
}
public static List<Integer> midOrderTraversalWithoutRecursion(TreeNode root){
List<Integer> ans = new LinkedList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode p = root;
while(p != null || !stack.isEmpty()){
if(p != null){
stack.push(p);
p = p.left;
}else{
p = stack.pop();
ans.add(p.val); //中序遍历出栈加入到ans中
p = p.right;
}
}
return ans;
}
public static List<Integer> postOrderTraversalWithoutRecursion(TreeNode root) {
List<Integer> ans = new LinkedList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode p = root;
TreeNode last = null;
while (p != null || !stack.isEmpty()){
if(p != null){
stack.push(p);
p = p.left;
}else{
if(stack.peek().right == null || stack.peek().right == last){ //右儿子不需要遍历
last = stack.pop();
ans.add(last.val);
}else{
p = stack.peek().right; //右儿子需要遍历
}
}
}
return ans;
}
public static void main(String[] args) {
TreeNode t = new TreeNode(1);
t.left = new TreeNode(2);
t.right = new TreeNode(3);
t.left.right = new TreeNode(4);
t.right.left = new TreeNode(5);
List<Integer> test1 = preOrderTraversalWithoutRecursion(t);
List<Integer> test2 = midOrderTraversalWithoutRecursion(t);
List<Integer> test3 = postOrderTraversalWithoutRecursion(t);
System.out.println("前序遍历:");
for(Integer i:test1){
System.out.print(i+" ");
}
System.out.println();
System.out.println("中序遍历:");
for(Integer i:test2){
System.out.print(i+" ");
}
System.out.println();
System.out.println("后序遍历:");
for(Integer i:test3){
System.out.print(i+" ");
}
System.out.println();
}
}