tree traversal

二叉树遍历

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
public class  {

* 前序遍历递归版本
*
* @param root root
* @param result 结果集
*/
public static void preOrder0(TreeNode root, ArrayList<Integer> result) {
if (root != null) {
result.add(root.val);
preOrder0(root.left, result);
preOrder0(root.right, result);
}
}


* 中序遍历递归版本
*
* @param root root
* @param result 结果集
*/
public static void inOrder0(TreeNode root, ArrayList<Integer> result) {
if (root != null) {
inOrder0(root.left, result);
result.add(root.val);
inOrder0(root.right, result);
}
}


* 后序遍历递归版本
*
* @param root root
* @param result 结果集
*/
public static void postOrder0(TreeNode root, ArrayList<Integer> result) {
if (root != null) {
postOrder0(root.left, result);
postOrder0(root.right, result);
result.add(root.val);
}
}


* 前序遍历非递归版本
*
* @param root root
* @param result 结果集
*/
public static void preOrder(TreeNode root, ArrayList<Integer> result) {
if(root == null)return;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.empty()) {
TreeNode node = stack.pop();
result.add(node.val);
if (node.right != null) stack.push(node.right);
if (node.left != null) stack.push(node.left);
}
}



* 中序遍历非递归版本
*
* @param root root
* @param result 结果集
*/
public static void inOrder(TreeNode root, ArrayList<Integer> result) {
if(root == null)return;
Stack<TreeNode> stack = new Stack<>();
while (!stack.empty() || root != null) {
while (root != null) {
stack.push(root);
root = root.left;
}
root = stack.pop();
result.add(root.val);
root = root.right;
}
}


* 后续遍历非递归版本
* @param root root
* @param result 结果集
*/
public static void postOrder(TreeNode root, ArrayList<Integer> result){
if(root == null)return;
Stack<TreeNode> first = new Stack<>();
Stack<TreeNode> last = new Stack<>();
first.push(root);
while(!first.empty()){
TreeNode cur = first.pop();
last.push(cur);
if(cur.left != null)first.push(cur.left);
if(cur.right != null)first.push(cur.right);
}
while(!last.empty()){
result.add(last.pop().val);
}
}


* 层次遍历
* @param root root
* @param result 结果集
*/
public static void levelOrder(TreeNode root, ArrayList<Integer> result){
if(root == null)return;
LinkedList<TreeNode> queue = new LinkedList<>();
queue.add(root);
while(!queue.isEmpty()){
TreeNode cur = queue.poll();
result.add(cur.val);
if(cur.left!=null) queue.add(cur.left);
if(cur.right!=null) queue.add(cur.right);
}
}


* Morris 先序遍历
* 时间- O(N) 空间 - O(1)
* @param root root
* @param result 结果集
*
*/
public static void morrisPre(TreeNode root, ArrayList<Integer> result) {
if (root == null) return;
TreeNode cur = root;
TreeNode mostRight;
while (cur != null) {
mostRight = cur.left;
if (mostRight != null) {
while (mostRight.right != null && mostRight.right != cur) {
mostRight = mostRight.right;
}
if (mostRight.right == cur) {
mostRight.right = null;
} else {
result.add(cur.val);
mostRight.right = cur;
cur = cur.left;
continue;
}

}else{
result.add(cur.val);
}
cur = cur.right;
}
}
}