-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinaryTreeInorderTraversal.java
More file actions
38 lines (30 loc) · 1 KB
/
Copy pathBinaryTreeInorderTraversal.java
File metadata and controls
38 lines (30 loc) · 1 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
// Given a binary tree, return the inorder traversal of its nodes' values.
// See: https://leetcode.com/problems/binary-tree-inorder-traversal/
package leetcode.tree;
import java.util.ArrayList;
import java.util.List;
import leetcode.util.tree.TreeNode;
public class BinaryTreeInorderTraversal {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
dfs(root, result);
return result;
}
private void dfs(TreeNode root, List<Integer> res) {
if (root == null) {
return;
}
dfs(root.left, res);
res.add(root.val);
dfs(root.right, res);
}
public static void main(String[] args) {
BinaryTreeInorderTraversal sln = new BinaryTreeInorderTraversal();
TreeNode n1 = new TreeNode(1);
TreeNode n2 = new TreeNode(2);
TreeNode n3 = new TreeNode(3);
n1.right = n2;
n2.left = n3;
System.out.println(sln.inorderTraversal(n1));;
}
}