1.递归 public List<Integer> preorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<>(); accessTree(root,res); return res; } public void accessTree(TreeNode root,List<Integer>res){ if(root == null){ return; } res.add(root.val); accessTree(root.left,res); 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; }