-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode0094.java
More file actions
54 lines (47 loc) · 1.39 KB
/
Copy pathLeetCode0094.java
File metadata and controls
54 lines (47 loc) · 1.39 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
/* Binary Tree Inorder Traversal
* Input:
* 1
\
2
/
3
* Output: [1,3,2]
* */
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class LeetCode0094 {
public static int index = 0;
public static int[] TREE_VALUE = new int[]{1, 0, 2, 3, 0, 0, 0};
public static void main(String args[]) {
TreeNode root = new TreeNode();
root = TreeNode.createTree(root, index, TREE_VALUE);
System.out.println(inorderTraversal(root));
}
//Recursive
/*static List<Integer> res = new ArrayList<>();
public static List<Integer> inorderTraversal(TreeNode root) {
if (root != null) {
inorderTraversal(root.left);
res.add(root.val);
inorderTraversal(root.right);
}
return res;
}*/
//Stack
public static List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode curr = root;
while (curr != null || !stack.isEmpty()){
while (curr != null){
stack.push(curr);
curr = curr.left;
}
curr = stack.pop();
res.add(curr.val);
curr = curr.right;
}
return res;
}
}