-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpreorder.java
More file actions
47 lines (44 loc) · 1.51 KB
/
preorder.java
File metadata and controls
47 lines (44 loc) · 1.51 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
import java.util.*;
public class preorder {
public static void main(String[] args){
Codec obj = new Codec();
String tree = "1,N,2,3,N,N,N";
TreeNode root = obj.deserilize2(tree);
System.out.println(interativeTraverse(root));
}
// method 1: 跟inorder traverse的方法很相近
// preorder: v -> left -> right
// iterative method using a stack
public static List<Integer> interativeTraverse(TreeNode root){
Stack<TreeNode> stack = new Stack<>();
List<Integer> res = new ArrayList<>();
TreeNode cur = root;
while(cur != null || !stack.isEmpty()){
while(cur != null){
res.add(cur.val);
stack.push(cur);
cur = cur.left;
}
// at this time: cur == null
// need to pop one and add new nodes into the stack
cur = stack.pop();
cur = cur.right;
}
return res;
}
// method 2: 发现这种方法的时候三观都碎了
// 很适合继续发展成n-ary
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if(root == null) return res;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode cur = stack.pop();
res.add(cur.val);
if(cur.right != null) stack.push(cur.right);
if(cur.left != null) stack.push(cur.left);
}
return res;
}
}