-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreeInorderTraversal.java
More file actions
44 lines (40 loc) · 1.02 KB
/
BinaryTreeInorderTraversal.java
File metadata and controls
44 lines (40 loc) · 1.02 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
package medium;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import util.TreeNode;
/**
*
* ClassName: BinaryTreeInorderTraversal
* @author chenyiAlone
* Create Time: 2019/01/31 20:54:40
* Description: No.94
*
* Given a binary tree, return the inorder traversal of its nodes' values.
Example:
Input: [1,null,2,3]
1
\
2
/
3
Output: [1,3,2]
*/
public class BinaryTreeInorderTraversal {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode temp = root;
while (temp != null || !stack.isEmpty()) {
if (temp != null) {
stack.push(temp);
temp = temp.left;
} else {
temp = stack.pop();
res.add(temp.val);
temp = temp.right;
}
}
return res;
}
}