-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path94.java
More file actions
54 lines (53 loc) · 1.56 KB
/
94.java
File metadata and controls
54 lines (53 loc) · 1.56 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
__________________________________________________________________________________________________
sample 0 ms submission
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
final List<Integer> ret = new ArrayList<>();
inorderTraversal(root, ret);
return ret;
}
private void inorderTraversal(TreeNode root, List<Integer> ret) {
if (root == null) return;
inorderTraversal(root.left, ret);
ret.add(root.val);
inorderTraversal(root.right, ret);
}
}
__________________________________________________________________________________________________
sample 33424 kb submission
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new LinkedList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while(cur!=null || !stack.isEmpty()){
while(cur!=null){
stack.push(cur);
cur = cur.left;
}
cur =stack.pop();
res.add(cur.val);
cur = cur.right;
}
return res;
}
}
__________________________________________________________________________________________________