Question
Given a binary tree, return the inorder traversal of its nodes’ values.
For example:
Given binary tree {1,#,2,3},
1
\
2
/
3
return [1,3,2].
Code
public void get(TreeNode root, List<Integer> results) {
if (root == null) {
return;
}
get(root.left, results);
results.add(root.val);
get(root.right, results);
}
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> results = new ArrayList<>();
get(root, results);
return results;
}