-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinorder.java
More file actions
42 lines (40 loc) · 1.28 KB
/
inorder.java
File metadata and controls
42 lines (40 loc) · 1.28 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
import java.util.*;
public class inorder {
public static void main(String[] args){
Codec obj = new Codec();
String tree = "1,N,2,3,N,N,N";
TreeNode root = obj.deserilize2(tree);
List<Integer> res = new ArrayList<>();
inorderTraversal(root,res);
System.out.println(res);
System.out.println(inorderInterative(root));
}
// recursive
public static void inorderTraversal(TreeNode root, List<Integer> res){
if(root == null){
return;
}
// inorder: left -> v -> right
inorderTraversal(root.left, res);
res.add(root.val);
inorderTraversal(root.right, res);
}
// iterative method using a stack
public static List<Integer> inorderInterative(TreeNode root){
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while(cur != null || !stack.isEmpty()){
while(cur != null){
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();
res.add(cur.val);
cur = cur.right;
}
return res;
}
}