題目:
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]
Follow up: Recursive solution is trivial, could you do it iteratively?
中序遍歷
AC:
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> resultList = new LinkedList<>();
if (root == null) {
return resultList;
}
helper(resultList, root);
return resultList;
}
private void helper(List<Integer> resultList, TreeNode root) {
if (root.left != null) {
helper(resultList, root.left);
}
resultList.add(root.val);
if (root.right != null) {
helper(resultList, root.right);
}
}