1.递归
时间复杂度O(n)
public List<Integer> inorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();accessTree(root,res);return res;}public void accessTree(TreeNode root,List<Integer>res){if(root == null){return;}accessTree(root.left,res);res.add(root.val);accessTree(root.right,res);}
2.循环迭代(栈)
public List<Integer> inorderTraversal(TreeNode root){List<Integer> res = new ArrayList<>();Deque<TreeNode> stack = new LinkedList<>();while (root!=null || !stack.isEmpty()){while (root!=null){stack.push(root);root = root.left;}root = stack.pop();res.add(root.val);root = root.right;}return res;}