public class InorderTraversal {
static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int val) {
this.val = val;
}
}
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
inorderTraversalHelper(root, result);
return result;
}
private void inorderTraversalHelper(TreeNode node, List<Integer> result) {
if (node == null) {
return;
}
inorderTraversalHelper(node.left, result);
result.add(node.val);
inorderTraversalHelper(node.right, result);
}
public static void main(String[] args) {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(4);
root.right = new TreeNode(2);
root.right.left = new TreeNode(3);
InorderTraversal solution = new InorderTraversal();
List<Integer> result = solution.inorderTraversal(root);
System.out.println(result);
}
}