题目
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree [1,null,2,3],
1
\
2
/
3
return [1,3,2].
Note: Recursive solution is trivial, could you do it iteratively?
答案
Recursive
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
inorderTraversal(list, root);
return list;
}
public void inorderTraversal(List<Integer> list, TreeNode root) {
if(root == null) return;
inorderTraversal(list, root.left);
list.add(root.val);
inorderTraversal(list, root.right);
}
}
Iterative