-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreeInorderTraversal.java
More file actions
35 lines (32 loc) · 1009 Bytes
/
BinaryTreeInorderTraversal.java
File metadata and controls
35 lines (32 loc) · 1009 Bytes
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
import common.TreeNode;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/*
* https://leetcode.com/problems/binary-tree-inorder-traversal/
*/
public class BinaryTreeInorderTraversal {
public List<Integer> inorderTraversal(TreeNode root) {
if (root == null) {
return Collections.emptyList();
}
List<Integer> ans = new LinkedList<>();
ans.addAll(inorderTraversal(root.left));
ans.add(root.val);
ans.addAll(inorderTraversal(root.right));
return ans;
}
public static void main(String[] args) {
System.out.println(new BinaryTreeInorderTraversal().inorderTraversal(
new TreeNode(
1,
null,
new TreeNode(
2,
new TreeNode(3),
null
)
)
)); // [1, 3, 2]
}
}